l代表字母,s代表空格,d代表数字,o代表其他字符,无论输入什么字符o的输出值都只等于0,还怎么解决
#include<stdio.h>
#include<stdlib.h>
int main()
{
char c;
int l = 0, s = 0, d = 0, o = 0;
printf("请输入字符\n");
while
((c = getchar()) != '\n')
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
l++;
else if (c >= '0' && c <= '9')
d++;
else if (c = ' ')
s++;
else
o++;
}
printf("l:%d\ns:%d\nd:%d\no:%d\n",l, s, d, o);
system("pause");
}
改成==试试:
else if (c = ' ')
那么一定要包含才可以使用吗?并非如此。前面说过了,包含不过是使用里面的声明,既然如何,我们自己声明怎么样?看下面的代码:
//hello.c,没有包含stdio.h
int printf (const char *__restrict __format, ...);
//extern int printf (const char *__restrict __format, ...);
int main(void)
{
printf("hello,编程珠玑\n");
return 0 ;
}
同样可以好好运行,因为你可以自己声明或者指定为外部声明。不过这样不建议,因为一旦出现自己声明的与实际的不符合,就可能导致意料不到的事情发生。