-
scanf and EOF
Hi all,
i have studied this following statement from a book "Each scanning function returns EOF if it encounters end-of-file before any conversion; otherwise it returns the number of successful conversions that were stored."
To test this i have written the following code
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
char c;
int ret=0;
ret = scanf("%c",&c);
printf("%c\n %d\n",c,ret);
printf("%d",EOF);
return 0;
}
i have given directly EOF character Ctrl + Z followed by return key as input. The output is ret = 1 instead of -1. where am i doing the mistake? i know i am doing a mistake some where.
thanks in advance,
regards,
satya
-
What OS are you on? In Cygwin as soon as I hit CTRL D it stops and produces a -1. I don't have to hit enter. Unix uses CTRL D, DOS uses CTRL Z.
Quzah.
-
Should ret not be 1? Scanf returns the number of items read if it scanned correctly.
-
Hi,
i tested first on Windows XP and the returned value is 1 when i entered CTRL Z followed by enter key. When i test on windows 7 and when i enter CTRL Z followed by enter key it returns -1. I need to test it again on windows XP, right now i do not have access to that PC, may be i will retest tomorrow and let you know.
sorry i missed the following point. i am using the QT software to write C programs.
thanks and regards,
satya