scanf and EOF

This is a discussion on scanf and EOF within the C Programming forums, part of the General Programming Boards category; Hi all, i have studied this following statement from a book "Each scanning function returns EOF if it encounters end-of-file ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    90

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Should ret not be 1? Scanf returns the number of items read if it scanned correctly.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    90
    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
    Last edited by Satya; 03-05-2012 at 10:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a name in scanf
    By taffy in forum C Programming
    Replies: 5
    Last Post: 03-24-2011, 12:00 PM
  2. scanf or gets?
    By 00Sven in forum C Programming
    Replies: 6
    Last Post: 02-14-2006, 03:07 PM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. scanf
    By beginner2003 in forum C Programming
    Replies: 5
    Last Post: 02-09-2003, 07:55 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21