fflush doesn't work

前端 未结 2 1392
予麋鹿
予麋鹿 2021-01-02 22:19

Why fflush(..) doesn\'t work to c2 and c0?
If I use the declaration c0 = 0 and c2 = 0 it works, but

2条回答
  •  旧时难觅i
    2021-01-02 22:57

    fflush(stdin) has undefined behavior.Use this henceforth to deal with the newline that remains in the stdin buffer while using scanf(),especially in cases when you need to read a character but the newline remaining in the buffer is automatically taken up as the character :

     while((c = getchar()) != '\n' && c != EOF);
    

    Here's what the cplusplusreference says about fflush() (You can verify the same from other sources as well,because too many veterans here on SO frown upon cplusplusreference though they fall short of condemning it altogether)

    ......In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).....

    http://www.cplusplus.com/reference/cstdio/fflush/

提交回复
热议问题