我尝试使用 fgets 根据用户输入的字符串调用不同的函数。我知道稍后我需要使用 strtok,因为会有空格,例如“load 12”。但现在,我对使用 strcmp 比较输入的字符串感到困惑。我知道 strcmp 可以这样使用:
int check;
char string[10] = "test";
check = strcmp(string, "test");
// Check will be 0 if true
if (check == 0)
{
printf("same string\n");
}
else
{
printf("not the same\n");
}
可以是像 true 和 false 这样的 bool 值吗?如果“test”实际上是“test”, bool 值变为true,那么我将在 if
语句中使用 bool 值。
最佳答案
您可以编写并使用包装器/辅助函数来实现此目标,如下所示:
bool are_equal(char* a, char* b)
{
return strcmp(a, b)==0;
}
关于我可以使用 strcmp 作为 bool 值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469898/