zoukankan      html  css  js  c++  java
  • scanf()函数原理

    一、三点说明

    1、用户输入的字符,会以ASCII码形式存储在键盘缓冲区;
    2、每调用一次scanf函数,就从键盘缓冲区读走一个字符,相当于清除缓冲区;
    3、若用户一次输入n个字符,则前n次调用scanf函数都不需要用户再次输入,直到把缓冲区的数据全部读取(清除)干净

    4、调用scanf()函数时,用户最后输入的回车也会储存在键盘缓冲区;(见程序示例2)

    二、程序示例1

     1 # include <stdio.h>
     2 
     3 int main()
     4 {
     5     char ch;
     6     while (1)
     7     {
     8         scanf("%c", &ch);
     9 
    10         switch(ch)
    11         {
    12             case '1':
    13                 printf("haha
    ");
    14                 break;
    15             case '2':
    16                 printf("cccccc
    ");
    17 //                fflush(stdin);    //清除缓冲区
    18                 break;
    19             case '3':
    20                 printf("555
    ");
    21                 break;
    22             case 'e':
    23                 return 0;
    24             default:
    25                 return 0;
    26         }
    27     }
    28 
    29     
    30 
    31     return 0;
    32 }
    33 
    34 /*
    35 程序在VC++6.0中的显示结果是:
    36 1235r
    37 haha
    38 cccccc
    39 555

    程序示例2

     1 # include <stdio.h>
     2 
     3 int main()
     4 {
     5     char c;
     6     scanf("%c", &c);
     7     printf("%d
    ", c);
     8 
     9     scanf("%c", &c);
    10     printf("%d
    ", c);
    11     
    12     return 0;
    13 }
    14 
    15 /*
    16 程序在VC++6.0中的显示结果是:
    17 1
    18 49
    19 10
    20 */

    上例中因为1对应的ASCII码是49,回车键对应的ASCII码是10,故有以上输出;

    第二个scanf从缓冲区读入了“回车”,显然这是我们不愿要的,如果要想清除这个垃圾值,只需要在第8行添加语句fflush(stdin)

    程序示例3

     1 #include <stdio.h>
     2 #include <conio.h>
     3 
     4 void main( void )
     5 {
     6    int integer;
     7    char string[81];
     8 
     9    /* Read each word as a string. */
    10    printf( "Enter a sentence of four words with scanf: " );
    11    for( integer = 0; integer < 4; integer++ )
    12    {
    13       scanf( "%s", string );
    14       printf( "%s
    ", string );
    15    }
    16 
    17    /* You must flush the input buffer before using gets. */
    18    fflush( stdin );
    19    printf( "Enter the same sentence with gets: " );
    20    gets( string );
    21    printf( "%s
    ", string );
    22 }

    三、清除缓冲区的几种方法

    我们使用多个scanf()的时候,如果输入缓冲区还有数据的话,那么scanf()就不会询问用户输入,而是直接就将输入缓冲区的内容拿出来用了,这就导致了前面的错误影响到后面的内容,为了隔离这种问题,需要通过各种方法将输入缓冲区的内容读出来(清除)

    1、fflush(stdin)

    在程序17行如果插入代码,依然输入1235r,则输出为 haha  cccccc

    此种方法对vc可以,但对xcode和linux不适用

    2、while+getchar

     while (ch=getchar() != ' ' && ch != 'EOF'),直到读取到缓冲区的换行或者空值

    四、关于scanf函数接受键盘的细节

    程序示例1

     1 #include <stdio.h>
     2 
     3 int main() 
     4 {    
     5    int a = 0, b =0;
     6    char d = 'a', e ='a';
     7    scanf("%d",&a);        //输入字符a到缓存,跳过接受,a=0
     8    scanf("%d",&b);        //输入字符a到缓存,跳过接受,b=0
     9    scanf("%c",&d);        //输入字符a到缓存,接受,d=a
    10    scanf("%c",&e);        //e接受换行符,ASCII为10
    11    printf("%d,%d,%c,%d
    ",a,b,d,e);
    12    return 0;
    13 }
    14 
    15 /*
    16 程序在VC++6.0中的显示结果是:
    17 a
    18 0,0,a,10
    19 */
  • 相关阅读:
    bootstrap 导航栏、输入框按钮组、栅格系统
    Python排序算法冒泡排序选择排序插入排序
    Python 解螺旋数组
    初到博客园,请多指教
    gcd, map for ocaml
    qsort for ocaml
    子序列和最大的问题
    将一个整数划分为非负整数的部分
    filter in Ocaml
    scheme中表只能操作头部带来的一个问题
  • 原文地址:https://www.cnblogs.com/shuaishuaidefeizhu/p/5886899.html
Copyright © 2011-2022 走看看