c 中的 If 和 Else 语句不起作用
If and Else statements in c not working
我已经在我的 raspberry pi 模型 b 上用 c 编写了这个小测试程序。
它编译没有错误,但是,if 语句从不工作:
#include <stdio.h>
#include <unistd.h>
int main(int argc, const char * argv[])
{
char *test = 0;
printf("Alpha or Beta\n");
scanf(" %s", test);
if (test == "Alpha")
{
printf("This is string one test\n");
printf("This is string two test\n");
}
else
{
printf("An error has occured\n");
}
return 0;
}
例如:
我将键入 Alpha,它总是会给我错误消息。
您不能使用“==”比较字符串。请改用 strcmp 函数。
http://en.cppreference.com/w/c/string/byte/strcmp
我已经在我的 raspberry pi 模型 b 上用 c 编写了这个小测试程序。
它编译没有错误,但是,if 语句从不工作:
#include <stdio.h>
#include <unistd.h>
int main(int argc, const char * argv[])
{
char *test = 0;
printf("Alpha or Beta\n");
scanf(" %s", test);
if (test == "Alpha")
{
printf("This is string one test\n");
printf("This is string two test\n");
}
else
{
printf("An error has occured\n");
}
return 0;
}
例如: 我将键入 Alpha,它总是会给我错误消息。
您不能使用“==”比较字符串。请改用 strcmp 函数。 http://en.cppreference.com/w/c/string/byte/strcmp