C語言scanf輸入char型別時遇到回車問題

2021-06-21 21:11:57 字數 615 閱讀 1187

今天在學c語言的時候遇到了scanf函式接收回車問題,不多說,先看源**:

float a = 0, b = 0;

char c;

printf("please input first number:\n");

scanf("%f",&a);

printf("please input second number:\n");

scanf("%f",&b);

printf("please input one of「+, - , * , /」\n");

scanf("%c",&c);

switch (c) else

break;

default:printf("input error\n");

break;

}

問題

1.將標記語句改為

scanf("\n%c",&c);
2.在標記語句之前新增語句 

getchar();

這倆中方法均可解決.

希望能對初學者有所幫助。自己也要繼續努力!!!

C語言使用scanf輸入

scanf 函式結構 scanf 函式被用來從stdin讀入資料,並且在程式中儲存資料。預設,這個stdin流是來著鍵盤輸入。scanf 的函式結構與printf 類似 int i scanf d i d 是乙個佔位符,說明這個地方有乙個變數,並且為整型變數 是個位址符,表明這個地方放置位址,i的意...

使用scanf輸入int或char資料

使用scanf輸入資料 scanf c c 輸入 100,實際唯讀了字元 1 printf c n c 輸出 1 printf d n c 輸出49.字元t的ascii值就是 49 說明 char 型別的變數,它儲存的是 字元對應的ascii碼值。int 變數的輸入 int a seanf d a ...

C語言格式輸入函式scanf 詳解

scanf函式稱為格式輸入函式,即按使用者指定的格式從鍵盤上把資料輸入到指定的變數之中。scanf函式是乙個標準庫函式,它的函式原型在標頭檔案 stdio.h 中。與printf函式相同,c語言也允許在使用scanf函式之前不必包含stdio.h檔案。scanf函式的一般形式為 scanf 格式控制...