我正在尝试开发一个小程序,如下所示: 当我运行该程序时,即使在正确的输入之后,它也会给出 else 中描述的输出。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char course;
printf("Enter Your Course Name: \n");
scanf(" %s", &course);
if (course == 'TOEFL') {
printf("Yes, you are eligible \n");
} else {
printf("You Can Not Join Us \n");
}
return 0;
}
最佳答案
你的意思是
scanf(" %c", &course);
而且,
c 中的字符串必须用双引号括起来,表达式
'TOEFL'
必须生成有关多字符常量的警告,不要忽略它。c 中的字符串一次比较一个字符,因此您需要使用名为
strcmp()
的函数来进行比较。要读取字符串,您需要一个数组来存储它,是的,
"%s"
说明符char cours[100]; scanf("%99s", course); if (strcmp(course, "TOEFL") == 0) ...
关于c - 我做错了什么?使用 scanf 的 if 语句无法与输入正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42920525/