为何这一程序无法通过预设条件退出?


#include<stdio.h>
#include<math.h>
void main()
{
    int i = 0;
    char c;
    while (1) 
    {
        c = '\0';
        while (c != 13 && c != 27)
        {
            c = getchar();
            printf("%c\n", c);
        }
        if (c == 27)
        {
            break;
        }
        i++;
        printf("The No.is %d\n", i);
    }
    printf("The end");

}

该程序为何无法通过设定好的输入esc键和回车退出?

首先,getchar是敲击回车符后才会刷新缓存,所以如果不输入回车符的话,内层的while会一直卡在getchar()函数这里。
因为回车符一般是\n,\n的ASCII码是10,13是\r的ASCII吗,\r是换行符。
所以输入esc键是没用的,因为最后的字符肯定是\n,c的值就是10,不会等于27的,所以不会退出。