读书人

大姐们帮忙看看为什么这个程序出不来s

发布时间: 2012-10-06 17:34:01 作者: rapoo

各位大哥大姐们帮忙看看为什么这个程序出不来sum值啊?
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char sum=-1;
int c;
/*
**Read the characters one by one ,and add them to the sum.
*/

while ((c=getchar())!=EOF)
{
putchar(c);
sum+=c;
}
printf("%d\n",sum);
return EXIT_SUCCESS;
}

[解决办法]
1、char的范围有限
2、char sum=-1;//没明白等于-1是什么意思

[解决办法]
我是新手,回答错了请见谅
[解决办法]
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char sum=-1;
int c;
/*
**Read the characters one by one ,and add them to the sum.
*/

while ((c=getchar())!=EOF)
{
putchar(c);
sum+=c;
printf("%d\n",sum);
}

return EXIT_SUCCESS;
}
[解决办法]
我的印象char 0-255.

感觉你的程序中sum 和 c的类型写反了。

你代码中sum+=c;这里存在隐式的类型转换。

读书人网 >C语言

热点推荐