[討論] printf before scanf 請益

看板C_and_CPP作者 (沒有暱稱)時間4年前 (2019/08/22 18:04), 編輯推噓0(003)
留言3則, 3人參與, 4年前最新討論串1/1
Linux gcc 我的stdout的緩衝應該是行緩衝(_IOLBF),所以如果沒有換行符,那printf的內容 就會在碰到retrun的時候才被一併flush出來。 但為什麼我這邊下printf("asdf"), asdf會馬上顯示? scanf的buffer不會跟printf共用吧? #include<stdio.h> #include<unistd.h> #include<stdlib.h> void foo(){ fprintf(stderr,"foo says good bye\n");} void bar(){ fprintf(stderr,"bar says bye.\n");} int main(int argc , char ** argv){ atexit(foo); atexit(bar); int x; int count = 0; printf("dfdfdfdfdfd"); scanf("%d",&x); sleep(2); if(argc > 1 && strcmp(argv[1],"exit") == 0) exit(0); if(argc > 1 && strcmp(argv[1],"_exit") == 0) _exit(0); return 0; } 且為什麼連結裡的結果卻要等到scanf的內容執行完才顯示第一個printf的資料? https://stackoverflow.com/questions/16877264/c-c-printf-before-scanf-issue 差在哪裡呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.75.14.193 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1566468289.A.BF3.html

08/24 00:02, 4年前 , 1F
有被優化成puts嗎
08/24 00:02, 1F

08/24 03:19, 4年前 , 2F
哪裡有asdf
08/24 03:19, 2F

08/24 23:06, 4年前 , 3F
講錯 dfdfdfdfd
08/24 23:06, 3F
文章代碼(AID): #1TNcZ1lp (C_and_CPP)