Enter键的ASCII码是10

来源:百度知道 编辑:UC知道 时间:2024/09/21 18:40:58
#include <stdio.h>

int main(void)
{
int count=0;
char beat;

scanf("%c",&beat);
while(beat==10)
{
count=count+1;
scanf("%c",&beat);
}

printf("count=%d \n",count);

return 0;
}
这个程序怎么样运行?为什么我运行时都没有终止?无论输入什么后按回车键都没有出现“count=”或者结束语句“Please enter any to end”的语句呢?谢谢了

这个程序可以运行的啊,怎么楼主会说运行都没有终止呢?

下面是运行结果:
当输入a后回车时,运行结果为:
a
count=0
Press any key to continue

当直接两个回车后,再输入a后回车时,运行结果为:

a
count=2
Press any key to continue

看到前面有两行空格了没?而count==2,这是因为:当只输入回车时,scanf取得beat==10,所以执行while循环,两次while循环后count等于2,

输入a回车后,scanf取得beat等于a,while循环条件不满足,不执行while循环,程序退出。

但是如果楼主想做的功能是统计输入字符是个数的话,
上面程序就是while循环的判断条件出错了,应该是
while(beat!=10)
{
count=count+1;
scanf("%c",&beat);
}

希望能对楼主有所帮助!!

while(beat==10)
{
count=count+1;
scanf("%c",&beat);
}
循环条件错了
while(beat!=10)
{
count=count+1;
scanf("%c",&beat);
}

while(beat!=10)