Alex Rivera | Logout

While scanf!=EOF or scanf==1?

Asked 2010-11-11T22:39:33.237
10

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, rather than !EOF? I may have read this somewhere, or whatever, but I may have it wrong as well. What do other people think?

Edit
Report

1 Answer

0

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.

answered 2010-11-11T22:46:42.060

Your Answer