Chinaunix

标题: c习题编译时出现"空的字符常量",怎么修改?(已解决) [打印本页]

作者: amro    时间: 2008-01-27 21:54
标题: c习题编译时出现"空的字符常量",怎么修改?(已解决)
题目要求:统计各个数字,空白符以及所有其他字符出现的次数。


#include <stdio.h>

main()
{
   int c, i, nwhite, nother;
   int ndigit[10];

   nwhite = nother = 0;
   for (i = 0; i < 10; ++i)
      ndigit[i] = 0;


   while ((c = getchar()) != EOF)
          if (c >= '0' && c <= '9')
             ++ndigit[c - '0'];
          else if (c == '' || c == '\n' || c == '\t')
            ++nwhite;
          else
         ++nother;

   printf("digits = ");
   for (i = 0;  i < 10; ++i)
   printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);

}


系统提示:
amro:~/doc/c$ gcc -o 1.6.1.out 1.6.1.c
1.6.1.c:16:25: 错误: 空的字符常量


请问怎么修改呢?谢谢

[ 本帖最后由 amro 于 2008-1-28 18:29 编辑 ]
作者: caijimin    时间: 2008-01-27 23:44
c == ' '
可能写成''了,当中漏了空格(Space)
作者: langue    时间: 2008-01-27 23:47
c == ''


'' 是啥?
"" 等同于长度为1、内容只有一个 '\0' 的串;可是……你说 '' 等同于什么好呢?
作者: langue    时间: 2008-01-27 23:52
2楼正解;white space 包括制表符、换行符和空格符
作者: xi2008wang    时间: 2008-01-28 00:07
呵呵,K&C的例题
作者: FinalBSD    时间: 2008-01-28 18:14
c == '' 改成 c== '\0'
作者: amro    时间: 2008-01-28 18:29
原帖由 caijimin 于 2008-1-27 23:44 发表
c == ' '
可能写成''了,当中漏了空格(Space)


原帖由 FinalBSD 于 2008-1-28 18:14 发表
c == '' 改成 c== '\0'


两种方法都可以,已经可以用了  谢谢各位。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2