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?

- 53,498
- 9
- 91
- 140

- 19,515
- 28
- 127
- 217
-
Somewhat related: http://stackoverflow.com/questions/2970880/using-scanf-in-a-while-loop – Oliver Charlesworth Nov 11 '10 at 22:42
-
@Steve: heh. Academia does that to you... never noticed, and nice touch italicizing it... you obviously come from academia as well. :) – Dervin Thunk Nov 11 '10 at 22:43
-
I'm afraid not - just a solidly traditional English school education in the 70s. – Steve Townsend Nov 11 '10 at 22:52
-
If that was the infamous "grammar schools", you win. English schools trump anything in the English-language educational system. :) – Dervin Thunk Nov 11 '10 at 22:59
3 Answers
scanf
returns the number of items succesfully converted ... or EOF on error. So code the condition the way it makes sense.
scanfresult = scanf(...);
while (scanfresult != EOF) /* while scanf didn't error */
while (scanfresult == 1) /* while scanf performed 1 assignment */
while (scanfresult > 2) /* while scanf performed 3 or more assignments */
Contrived example
scanfresult = scanf("%d", &a);
/* type "forty two" */
if (scanfresult != EOF) /* not scanf error; runs, but `a` hasn't been assigned */;
if (scanfresult != 1) /* `a` hasn't been assigned */;
Edit: added another more contrived example
int a[5], b[5];
printf("Enter up to 5 pairs of numbers\n");
scanfresult = scanf("%d%d%d%d%d%d%d%d%d%d", a+0,b+0,a+1,b+1,a+2,b+2,a+3,b+3,a+4,b+4);
switch (scanfresult) {
case EOF: assert(0 && "this didn't happen"); break;
case 1: case 3: case 5: case 7: case 9:
printf("I said **pairs of numbers**\n");
break;
case 0:
printf("What am I supposed to do with no numbers?\n");
break;
default:
pairs = scanfresult / 2;
dealwithpairs(a, b, pairs);
break;
}

- 30,033
- 48
- 152
- 225

- 106,608
- 13
- 126
- 198
Depends what you want to do with malformed input - if your scan pattern isn't matched, you can get 0
returned. So if you handle that case outside the loop (for example if you treat it the same as an input error), then compare with 1
(or however many items there are in your scanf call).

- 273,490
- 39
- 460
- 699
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.

- 299,747
- 42
- 398
- 622
-
The quoted text is incorrect. Items read (but not stored) using formats like `%*d` are **not counted** in the return value. www.cplusplus.com does not seem to be a good source for accurate info about the C standard library. If you'd like an online reference you can link to, POSIX is authoritative; for standard C functions with POSIX extensions, it has the extension parts marked in "CX" blocks. See http://www.opengroup.org/onlinepubs/9699919799/functions/scanf.html – R.. GitHub STOP HELPING ICE Nov 11 '10 at 23:31