Use of EOF

This is a discussion on Use of EOF within the C Programming forums, part of the General Programming Boards category; I am a newbie at C programming. In a C script which counts characters in a given input I used ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    Question Use of EOF

    I am a newbie at C programming.

    In a C script which counts characters in a given input I used EOF to detect the end of input but it didn't work. the input keeps being displayed on the screen and no output is provided. Enter or Ctrl + C don't do!

    The script is as given below:

    #include <stdio.h>

    main()
    {
    int nc;
    nc = 0;
    while (getchar() != EOF)
    ++nc;
    printf("%d\n", nc);
    }

    How am I going to make it detect the EOF? What is EOF exactly?
    Last edited by Arjit; 01-10-2010 at 09:19 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Post your program code in [code][/code] bbcode tags.

    Quote Originally Posted by Arjit
    How am I going to make it detect the EOF? What is EOF exactly?
    Read the FAQ on Definition of EOF and how to use it effectively.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    If you want output aswell, add putchar(c) command into you while loop. Also, you'll need a variable for that.

    Code:
    int c;
    while((c = getchar()) != EOF)
    {
       putchar(c);
       nc++;
    }
    EOF is declared in stdio.h to signal an end of file. I has an value of -1, since normal ascii characters have values 0-255 (extended set).

    This also answers a question why shouldnt you store characters from input to a character type variable - because of EOF. A char variable can contain values -128 till 127(not 255), thats why int is used.
    Last edited by Tool; 01-10-2010 at 11:31 AM.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    EOF is ctrl + D on unix and ctrl + Z on windows

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    Thumbs up

    Thanks buddies! Specially 'Anarchy' for the real stuff I was looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while ((c = getchar()) != EOF), AND cntrl z
    By Roger in forum C Programming
    Replies: 8
    Last Post: 10-21-2009, 09:25 PM
  2. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 08:09 PM
  3. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  4. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM

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