Thread: Getchar

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Question Getchar

    I dont get this:
    c=getchar(). where c is an integer type. so getchar takes the input from a file or keyboard and put it into a numerical variable. how that? what does this function return? the ascii code for the input?

  2. #2
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    The getchar() function returns the next character from stdin. The character is read as an unsigned char that is converted to an integer.
    If the end of the file is reached, getchar() returns EOF. If an error is encountered, EOF is also returned.
    The getchar() function is often implemented as a macro.
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The reason why it returns an int is because of EOF. It's value is 32 bits and is 0xffffffff if i'm not mistaken. If you were to do something like:

    char c;
    c = getchar();
    if(c == EOF)

    This would be wrong. You would not know if you were getting the character 0xff or EOF since you shoved the value into a char. Check the return value as an int. If it's not EOF, treat it as a char.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Post

    Ok. Now another problem linked to strings. I read a string into a array of char and want to count the letters. i put the !='\0' condition in a while statement but still, when i type space it stop counting what i write after. It was supposed to stop counting when it got to end, means found '\0'. Some ideas? I still have none.
    Thank u.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post some code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM