【发布时间】:2020-11-16 18:54:21
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int end;
while(( end = getchar() ) != EOF ){
printf("%d\n",end);
}
system("pause");
return 0;
}
我想用这个代码打印字符的 ASCII 代码,但是每当我在它从我这里获取字符后运行代码时,它就会打印它的 ASCII 等价于十进制 10。例如,如果我运行代码并传递“a”,它将打印 97 和 10。为什么它打印 10,这也发生在所有其他字符上。 感谢您的回答和作为后续问题,当我在输入字符计数器的值增加 2 后添加计数器时,为什么会发生这种情况
#include <stdio.h>
#include <stdlib.h>
int main()
{
int end;
int count=0;
while(( end = getchar() ) != EOF ){
printf("%d\n",end);
count++;
printf("counter is now %d\n",count);
}
system("pause");
return 0;
}
【问题讨论】:
-
如果您使用的是 ASCII,我会希望那里有 10,因为那是换行符
'\n'的数值 -
你按回车键了吧?
-
补充问题:您想要/期望它打印什么?
%d表示整数以十进制打印,例如'\r'将打印 13(即 ASCII CR,或回车),依此类推。