Thread: EOF and getchar() problem

  1. #1
    Registered User
    Join Date
    Oct 2012

    EOF and getchar() problem

    hi guys

    I'm challenged to write a code for the following:

    write a text up to 1000 characters, and get it back. the end of the text is detected though EOF on a new line or reaching 1000 characters.(EOF=ctr-Z on a new line)

    I mainly have problem how to bring the EOF work probably. it doesn't react when I press ctrl-z directly . could you see the problem in the code?

    int i;
        char Text[1000];
     printf("write the text:\n");
     Text[i]=getchar();                  //read every sign
     if((Text[i]=getchar()== '26' || i==1000)){   //26 is the ASCII code for ctrl-z
        return EXIT_SUCCESS;

  2. #2
    Registered User
    Join Date
    Mar 2011
    check two things: operator precedence of '==' vs '=' vs '||', and what value is '26' (hint, its not 26 decimal). best to put parentheses around each term you want to isolate in your expression.
    run this snippet :
    #include <stdio.h>
    int main(int argc,char *argv[])
    	int a = '26';
        return 0;
    edit: read the second paragraph in this link :
    C syntax - Wikipedia, the free encyclopedia
    Last edited by dmh2000; 06-20-2013 at 02:49 PM.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Nice, France
    Read twice the post of dmh and then take a look here, in order to see how one uses EOF. It was from the very first occurrences of EOF in our ip course, so I think it can help.
    Code - functions and small libraries I use

    It’s 2014 and I still use printf() for debugging.

    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Oct 2012
    Thanks guys!! it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with getchar()
    By blob84 in forum C Programming
    Replies: 2
    Last Post: 08-23-2010, 04:54 PM
  2. Problem with getchar()
    By SasDutta in forum C Programming
    Replies: 3
    Last Post: 03-31-2010, 10:33 AM
  3. Help getchar() problem
    By rob90 in forum C Programming
    Replies: 7
    Last Post: 01-28-2010, 11:26 AM
  4. Problem with getchar()
    By georgio777 in forum C Programming
    Replies: 6
    Last Post: 11-09-2009, 01:58 AM
  5. A getchar problem
    By ch4 in forum C Programming
    Replies: 2
    Last Post: 10-29-2007, 03:39 AM