【发布时间】:2017-07-06 00:23:49
【问题描述】:
我在使用 getchar() 和文件重定向进行比较时遇到问题。
我有一个类似这样的代码:
char result = getchar(); // getchar returns the next char in the file
int linecount = 0;
if (result == "\n") {
linecount++;
}
但我在编译时收到警告。它说我不能将 int 与指针进行比较,但据我了解,结果是 char,“\n”也是如此,所以我真的很困惑。我也可以使用 printf("%c", result") 并且它工作正常,这意味着结果是一个字符。有谁知道我为什么会收到这个错误?谢谢!另外,运行代码,linecount 将始终返回 0即使我用作输入的文件中的第一个字符是换行符。
【问题讨论】:
-
"\n"-->'\n'。期间。 -
哦,还有
char result-->int result -
确实
getchar返回int您将其分配给char然后与char*进行比较。 -
好的,非常感谢您的帮助!
-
1)
getchar()返回int,而不是char2) "\n" 声明一个多字节字符串。该代码应与字符'\n'进行比较