c语言中只读取输入的一行字符的首个字符

 

测试程序1:

#include <stdio.h>

int main(void)
{
    char ch;
    
    printf("please input an character: \n");
    
    while((ch = getchar()) != '#')
        printf("xxx\n");
        
    return 0; 
}

 

 

测试程序2

#include <stdio.h>

int main(void)
{
    char ch;
    
    printf("please input an character: \n");
    
    while((ch = getchar()) != '#')
    {
        printf("xxx\n");
        while(ch = getchar() != '\n')    // 为什么起作用?????
            continue;
    }
        
    return 0; 
}

 

posted @ 2021-08-25 23:07  小鲨鱼2018  阅读(366)  评论(0编辑  收藏  举报