日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[C语言] 混合or连续使用getchar,scanf所出现的错误

發布時間:2023/12/16 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [C语言] 混合or连续使用getchar,scanf所出现的错误 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[C語言] 混合or連續使用getchar,scanf所出現的錯誤

getchar

getchar()的作用是從系統隱含指定的輸入設備(即終端鍵盤)輸入一個字符,按回車鍵表示輸入結束。

測試getchar的具體用法

getchar()每次只能讀取一個字符,我們知道如果我們一次性輸入多個字符,getchar()只會讀取第一個字符,剩余的字符保存到標準緩沖區里
我們用于測試的源代碼如下:

#include <stdio.h> #include <stdlib.h> int main() {char a,b;printf(" Please press a key and then press enter:");a=getchar();printf(" You pressed:");putchar(a);printf("\n Please press a key and then press enter:");b=getchar();printf("\n You pressed:");putchar(b);} INPUT:abc OUTPUT:Please press a key and then press enter:abcYou pressed:aPlease press a key and then press enter:You pressed:b

可以看到程序直接跳過了第二條getchar()的輸入而取出標準緩沖區里的b輸出
如果我們只輸入一個字符呢?
我們以為程序應該是這樣的情況

Please press a key and then press enter:a You pressed:a Please press a key and then press enter:_

光標停留在第二個getchar()等待我們輸入第二個值,然而實際情況卻是這樣的

INPUT: a OUTPUT: Please press a key and then press enter:a You pressed:a Please press a key and then press enter: You pressed:

程序仍跳過了第二個getchar()直接結束了,看似好像第二個getcher()既沒輸入又沒輸出而程序就這樣結束了
其中的原因就在于getchar()是從標準緩沖區中讀取輸入的字符的,所以像上面那樣寫的情況下,字符型變量b接收的就是輸入a之后輸入的回車鍵

我們可以經行如下測試來消除那個回車鍵

  • 通過增加一個getchar()抵消回車鍵;
  • char a,b;printf(" Please press a key and then press enter:");a=getchar();printf(" You pressed:");putchar(a);getchar();printf("\n Please press a key and then press enter:");b=getchar();printf(" You pressed:");putchar(b);
  • 用 fflush(stdin);語句來清空緩存區;
  • char a,b;printf(" Please press a key and then press enter:");a=getchar();printf(" You pressed:");putchar(a);fflush(stdin);printf("\n Please press a key and then press enter:");b=getchar();printf(" You pressed:");putchar(b);

    用fflush(stdin);語句的作用可不僅僅能消除回車鍵,他能消除存在緩沖區里的所有字符,性質優良

    INPUT:abczxc OUTPUT:Please press a key and then press enter:abcYou pressed:aPlease press a key and then press enter:zxcYou pressed:z

    scanf

    scanf的作用是按指定格式要求和數據類型,讀入若干數據給的相應的變量
    借用我們用來測試getchar的代碼稍作改動,其得到的結果與getchar一樣

    char a,b;printf(" Please press a key and then press enter:");scanf("%c",&a);printf(" You pressed:");printf("%c",a);printf("\n Please press a key and then press enter:");scanf("%c",&b);printf("\n You pressed:");printf("%c",b);
  • scanf的改進方式除了上面兩種還可以通過改第二個scanf("%c",&b);變成
    scanf(“Space%c”,&b);用Space來抵消掉那個回車鍵;
  • 我們采取用%*c的方式消除回車鍵等修飾符;
  • char a,b;printf(" Please press a key and then press enter:");scanf("%c",&a);printf(" You pressed:");printf("%c",a);scanf("%*c");printf("\n Please press a key and then press enter:");scanf("%c",&b);printf(" You pressed:");printf("%c",b);

    但上述是字符型的變量,現在我們對int型變量經行測試

    int a,b;printf(" Please press a key and then press enter:");scanf("%d",&a);printf(" You pressed:");printf("%d",a);printf("\n Please press a key and then press enter:");scanf("%d",&b);printf(" You pressed:");printf("%d",b); INPUT:1224 OUTPUT:Please press a key and then press enter:12You pressed:12Please press a key and then press enter:24You pressed:24

    這樣的情況是我們見的最多的,經行兩次輸入
    又如果這樣

    INPUT:12 24 OUTPUT:Please press a key and then press enter:12You pressed:12Please press a key and then press enter: You pressed:24

    預料之中

    混合使用getchar和scanf

    int a,b;printf(" Please press a key and then press enter:");a=getchar();printf(" You pressed:");putchar(a);printf("\n Please press a key and then press enter:");scanf("%d",&b);printf(" You pressed:");printf("%d",b);

    效果跟前面基本相似,就是注意getchar只能讀取一個字符
    然而

    INPUT:ab OUTPUT:Please press a key and then press enter:aYou pressed:aPlease press a key and then press enter:bYou pressed:8

    通過debug窗口,我們可以看到8是b的初始化隨機數據,如果在初始化的時候int b=0;第二個press出來的就是0 ,這是怎么回事呢?
    我們注意到scanf("%d",&b);當輸入類型與制定的數據類型不統一時,將無法經行數據輸入

    本人小菜一枚,學識淺薄,言辭簡陋,如有不正確或是可以改進之處,歡迎大家指出,小生不勝感激!

    總結

    以上是生活随笔為你收集整理的[C语言] 混合or连续使用getchar,scanf所出现的错误的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。