【发布时间】:2013-09-19 12:51:18
【问题描述】:
我有这个简单的程序:
#include <stdio.h>
int main()
{
int c;
while ( ( c = getchar()) != EOF)
printf("%d %c\n", c, c);
return 1;
}
但是由于某种原因,在执行时我最后得到了一个额外的值 10:
a
97 a
10
b
98 b
10
abc
97 a
98 b
99 c
10
值 10 是什么?它从何而来?如何阻止它发生?
解决方案:
#include <stdio.h>
#include <ctype.h>
int main()
{
int c;
while ( ( c = getchar()) != EOF)
{
if ( isprint (c))
{
printf("%d %c\n", c, c);
}
}
return 1;
}
【问题讨论】:
-
10是换行符的 ascii 代码 (\n)