Reputation: 920
I have two pieces of codes to test how the two console I/O functions, getchar() & scanf(), handle the EOF. But I still do not have a clear comprehension about the actual operations behind the outputs and their behaviors. Can someone explains that for me? Thanks a lot! (I am using Windows OS)
// 1st piece of Code
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char ch;
while ((ch=getchar()) != EOF)
{
putchar(toupper(ch));
}
return 0;
}
If I type
abc
or
abc(ctrl+z)
The program will have the same outputs:
ABC
// 2nd piece of Code
#include<stdio.h>
int main(void)
{
int x;
while(scanf("%d",&x) != EOF)
{
/*Ctrl + z + Enter*/
printf("x=%d\n",x);
}
return 0;
}
If I type
123
The program will output:
x=123
Otherwise, if I type
123(ctrl+z)
The program will have an infinite output:
x=123
x=123
x=123
x=123
...
Upvotes: 1
Views: 314
Reputation: 108986
getchar()
returns the value of the character converted to unsigned char
or EOF
in case of error.
The error can be "end of file" or something else; usually (for getchar()
) the programmer does not care about the error, just that an error happened.
scanf()
returns the number of values matched and assigned (basically the number of %
in the format string) or EOF
in case of error. Note that the number can be less than the number of %
in case, for example, of badly formatted input
Just like for getchar()
the error can be "end of file" or something else. Particularly, reading less than the number of %
is not an error.
So you may prefer to test for the correct number of assignments rather than testing for error in scanf()
#include <stdio.h>
int main(void) {
int x;
while (scanf("%d", &x) != 1) {
/*Ctrl + z + Enter*/
printf("x=%d\n", x);
}
return 0;
}
Upvotes: 1
Reputation: 409482
The problem is that on Windows the EOF
is put into the input buffer like a normal character (with the encoded value 26
).
When reading character by character (with e.g. getchar
) this is handled by the Windows run-time library. But it doesn't work like that with scanf
because when scanf
parses the input it's like another character. And as a non-digit it's an invalid character for te "%d"
format, leading to your scanf
Call to return 0
instead of EOF
(since it's not parsed by the format).
One way to solve it is to press the Ctrl-Z sequence on its own new line.
Another (and more reliable) way to solve it is to check that scanf
returns the number of formats you have in the string. In your case you should compare against 1
(as you have one format specifier).
Upvotes: 0