[問題]請問while(scanf("%d",&sec)!=0)的問題?

看板C_and_CPP作者 (我的雞巴女友)時間6年前 (2018/02/16 01:41), 編輯推噓7(7010)
留言17則, 11人參與, 6年前最新討論串1/1
請問各位下面這兩個簡單的程式為什麼預期結果會不一樣 #include <stdio.h> const int S_PER_M=60; const int S_PER_H=3600; const double M_PER_K=0.6213; int main(int argc, char **argv) { int sec; while(scanf("%d",&sec)!=0) { printf("FFF\n"); fflush(stdin); scanf("%d",&sec); } printf("Ending\n"); return 0; } 上面這個程式我不管怎麼按"0"都不會跳出while 但是下面這個卻可以 #include <stdio.h> const int S_PER_M=60; const int S_PER_H=3600; const double M_PER_K=0.6213; int main(int argc, char **argv) { int sec; scanf("%d",&sec); while(sec!=0) { printf("FFF\n"); fflush(stdin); scanf("%d",&sec); } printf("Ending\n"); return 0; } 請問while(scanf("%d",&sec)!=0) 這樣寫有什麼不對嗎??? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.204.100.59 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1518716510.A.F19.html

02/16 01:55, 6年前 , 1F
自己去看scanf的回傳值
02/16 01:55, 1F

02/16 01:58, 6年前 , 2F
這誤會大了
02/16 01:58, 2F

02/16 01:59, 6年前 , 3F
再說兩個程式又不一樣意思,結果不一樣也是很正常的吧
02/16 01:59, 3F

02/16 02:46, 6年前 , 4F
請跟我一起推廣
02/16 02:46, 4F

02/16 02:46, 6年前 , 5F
Scanf_s或bscanf
02/16 02:46, 5F

02/16 11:39, 6年前 , 6F
scanf回傳值是你正確讀入幾個輸入 你上面的狀況
02/16 11:39, 6F

02/16 11:39, 6年前 , 7F
是1 更詳細的請自己google
02/16 11:39, 7F

02/16 16:29, 6年前 , 8F
不要省那一行啦 沒比較漂亮 分開寫不就會逼你自己想回
02/16 16:29, 8F

02/16 16:29, 6年前 , 9F
傳值是啥了嗎
02/16 16:29, 9F

02/17 11:44, 6年前 , 10F
如果scanf會回傳讀入的值 那你&sec是在傳心酸的喔
02/17 11:44, 10F

02/17 12:42, 6年前 , 11F
推樓上
02/17 12:42, 11F

02/17 13:18, 6年前 , 12F
不能這麼說, 像 strncpy 就是同時會回傳你接到的結果
02/17 13:18, 12F

02/17 13:19, 6年前 , 13F
不過當然因為 C 的型別限制 scanf 不可能直接回傳 int
02/17 13:19, 13F

02/17 13:19, 6年前 , 14F
所以原 po 的要求根本毫無道理
02/17 13:19, 14F

02/17 15:40, 6年前 , 15F
使用function的時候了解他的return意義是基本功喔@@
02/17 15:40, 15F

02/19 09:44, 6年前 , 16F
while (scanf("%d", &sec), sec)
02/19 09:44, 16F

02/19 22:08, 6年前 , 17F
scanf跟strncpy是有什麼關係 你會像你家隔壁的王叔叔嗎
02/19 22:08, 17F
文章代碼(AID): #1QXSPUyP (C_and_CPP)