While scanf!=EOF or scanf==1?

前端 未结 3 470
故里飘歌
故里飘歌 2021-01-02 06:54

Ceteris paribus (well formed data, good buffering practices and what not), is there a reason why I prefer to loop while the return of scanf is 1, rathe

3条回答
  •  囚心锁ツ
    2021-01-02 07:12

    From http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

    On success, the function returns the number of items succesfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens. In the case of an input failure before any data could be successfully read, EOF is returned.

    The only way to be sure that you read the number of items intended is to compare the return value to that number.

提交回复
热议问题