[問題] scanf函數的回傳值

看板C_and_CPP (C/C++)作者時間6年前 (2019/05/06 16:42), 6年前編輯推噓1(102)
留言3則, 3人參與, 6年前最新討論串1/1
以下是C scanf函式的原型 函式原型:int scanf ( const char * format, ... ); 引數說明:%[*][width][modifiers]type %[*][寬度][長度修飾]資料型態 傳回值:成功配對之引數數目,失敗時將傳回 EOF 請問下面這兩支程式作對比 兩支程式執行後在命令提示字元下輸入相同格式 ss(整數) (整數) 第一支程式scanf函式回傳為TURE 可是第二支程式的第二個scanf函式回傳值卻是FALSE 是甚麼原因? 是因為scanf格式輸入錯誤? 如果是編譯怎麼會成功呢?? 感謝!!! (1). #include <stdio.h> #include <stdlib.h> int main () { int b,c; printf("請輸入整數:"); c=scanf("ss%d",&b); printf("\n\nscanf的回傳值為%d\n\n",c); printf("\n\n你的輸入為%d\n\n",b); system("pause"); return 0; } (2). #include <stdio.h> #include <stdlib.h> int main () { int a,b,c; printf("請輸入整數:"); scanf("%d",&a); printf("\n\n你的輸入為%d\n\n",a); printf("請輸入整數:"); c=scanf("ss%d",&b); printf("\n\n第二個scanf的回傳值為%d",c); printf("\n\n你的輸入為%d\n\n",b); system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.231.238 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1557132145.A.440.html

05/06 18:14, 6年前 , 1F
你的順序是不是反了?
05/06 18:14, 1F

05/06 20:55, 6年前 , 2F
我想是換行問題?
05/06 20:55, 2F

05/07 02:54, 6年前 , 3F
%d讀完後 \n還留在buffer中
05/07 02:54, 3F
※ 編輯: a27839579 (223.137.231.238), 05/07/2019 17:06:10
文章代碼(AID): #1Sp_DnH0 (C_and_CPP)
文章代碼(AID): #1Sp_DnH0 (C_and_CPP)