【发布时间】:2010-01-01 10:57:27
【问题描述】:
以下程序无法按我的预期运行。
#include <string.h>
#include <stdio.h>
int main()
{
const char *c = "abcdef";
// prints 'f' as expected
printf("%c\n", c[5]);
// comparison fails, does not print "yes"
if (c[5] == 'f')
printf("yes");
return 0;
}
如何将字符串文字中的字符与字符值进行比较?是否可以不使用 ASCII 相关函数,即 chr() 或 ord() 假设它们存在。 (我记得在 Pascal 时代使用它们)
谢谢
【问题讨论】:
-
试试
printf("yes\n");或puts("yes");。你不需要string.h。 -
好吧,我觉得自己很笨。我错过了,正如马特·格里尔指出的那样