C语言学习笔记——scanf("%c",&ch) 读取字符

来源:互联网 发布:mac系统如何剪切文件 编辑:程序博客网 时间:2024/05/17 02:34
scanf("%c", &ch)  读入由终端输入的第一个字符。输入“Enter”后执行函数。因此每次输入n个字符时,I/O缓冲区中实际存储了n+1个字符,即n个输入的字符和一个换行字符(\n)。而scanf("%c", &ch)仅仅读取第一个输入的字符,字后的字符(包括'\n')会驻留在缓冲区中。如果不清除缓冲区内容,则在程序运行到下一个scanf("%c", &ch)时,直接从缓冲区中读取字符,而不需要再次输入,直到缓冲区中的内容被清除。换句话说,只要I/O缓冲区中存在字符,scanf("%c", &ch)就会直接读取第一个字符,并且不会判断读入的字符是什么。
   例如,程序
#include <stdio.h>#include <string.h>#include <ctype.h>int main(int argc, char *argv[]){     char ch = '\0';     do     {         printf("Input a char:");         scanf("%c", &ch);         printf("The char is %c.\n", ch);     }while(toupper(ch) != 'N');      return 0;}


    当输入 a时,实际上输入了字符 'a' 和字符 '\n'
    输出的结果将是:
  1 Input a char:a                       ------>    输入'a' + '\n'      I/O缓冲区内容:a\n
  2 The char is a.                       ------>    打印输入的字符'a'     I/O缓冲区内容:\n         循环第一遍
  3 Input a char:The char is            
  4 .                                              ------>    打印输入的字符'\n'    I/O缓冲区内容:(被清空)    循环第二遍
  5 Input a char:

    从输出结果的第3行可以看出,当循环第二遍的时候,没有输入任何字符,程序就打印了字符'\n'。
    同理,如果一次性输入字符 abcd,则缓冲区中的字符为 'a','b','c','d','\n'
    程序将输出:
  1 Input a char:abcd                       ------>    输入'abcd' + '\n'      I/O缓冲区内容:abcd\n
  2 The char is a.                              ------>    打印输入的字符'a'     I/O缓冲区内容:bcd\n      循环第一遍
  3 Input a char:The char is b.        ------>    打印输入的字符'b'     I/O缓冲区内容:cd\n       循环第二遍
  4 Input a char:The char is c.        ------>    打印输入的字符'c'     I/O缓冲区内容:d\n        循环第三遍
  5 Input a char:The char is d.        ------>    打印输入的字符'd'     I/O缓冲区内容:\n         循环第四遍
  6 Input a char:The char is
  7 .                                                     ------>    打印输入的字符'a'     I/O缓冲区内容:(被清空)    循环第五遍
  8 Input a char:

    再例如下面代码:
#include <stdio.h>#include <string.h>#include <ctype.h>int main(int argc, char *argv[]){    int a = -1;    char ch = '\0';    char word[80];    printf("Input a num:");    scanf("%d", &a);    printf("The num is %d\n", a);       printf("Input a char:");    scanf("%c", &ch);    printf("The char is %c.\n", ch);       printf("Input a word");    scanf("%s",word);    printf("The word is %s\n", word);    printf("Input a char");    scanf("%s",&ch);    printf("The char is %s\n", char);    return 0;}


    执行后会输出
  1 Input a num:982                       ------>  输入982+Enter            I/O缓冲区内容:982\n
  2 The num is 982.                       ------>  打印982                     I/O缓冲区内容:\n
  3 Input a char:The char is             
  4 .                                                   ------>  没有等待输入字符,直接打印\n    I/O缓冲区内容:(清空)
  5 Input a word:alex                  ------>  输入alex+Enter            I/O缓冲区内容:alex\n
  6 The word is alex.                     ------>  打印alex            I/O缓冲区内容:\n
  7 Input a char:The char is        
  8 .                                                   ------>  没有等待输入字符,直接打印"\n"    I/O缓冲区内容:(清空)

    由此可见,缓冲区结尾的字符'\n'在如字符串或者数字等时是不被scanf()读入变量的。因此要着重注意在使用scanf函数时对'\n'的处理。可以使用下面的方法屏蔽掉'\n'在缓冲区中的驻留。
    在scanf函数的格式字符串的末尾增加 %*c。%*c 表示从缓冲区中读取字符,但是不赋值给任何变量。
    例如下面的代码:
    int a = 0;
    scanf("%d%*c",a);
    或者
    char word[LEN];
    scanf("%s%*c",word);

    如果不能确定上次从终端获取输入后,缓冲区内是否还有'\n'驻留。可以在scanf函数的格式字符串的最前面添加一个空格。

    *'\n'驻留缓冲区所导致的问题 目前仅发现在 使用scanf读入单个字符时存在。*
原创粉丝点击