Thread: Use of EOF

  1. #1
    Registered User
    Join Date
    Jan 2010

    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>

    int nc;
    nc = 0;
    while (getchar() != EOF)
    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
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    int c;
    while((c = getchar()) != EOF)
    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
    EOF is ctrl + D on unix and ctrl + Z on windows

  5. #5
    Registered User
    Join Date
    Jan 2010

    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
Website Security Test