Thread: Getchar and getch different behaviors

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    2

    Getchar and getch different behaviors

    Hello all
    I'm wondering why getchar passes EOF character when pressing ctrl Z char, but getch gives 26?
    Or getchar gives 10 value when the user presses enter key, and getch gives 13 value
    Who knows?
    Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    'You see things; and you say "Why?" But I dream things that never were; and I say "Why not?"' ~ George Bernard Shaw, Back to Methuselah, Act I, Selected Plays with Prefaces, vol. 2, p. 7 (1949)

    Quote Originally Posted by Eshraghi6
    I'm wondering why getchar passes EOF character when pressing ctrl Z char, but getch gives 26?
    There isn't necessarily an "EOF character" (and there probably isn't). Rather, the CTRL+Z is to trigger the end-of-file condition, as if the end of the file has been reached, even though with interactive I/O on a command prompt, there isn't really the notion of a "file" as you might understand it. That you observed a character with the decimal value of 26 when using getch would indicate that the Windows command line might be using that character (or it may be the initial character of a sequence thereof) for that purpose. getchar, on the other hand, returns the constant named EOF specifically as a negative value (recall getchar returns an int) that is outside the range of possible characters (which are converted to unsigned char in the context of getchar).

    Quote Originally Posted by Eshraghi6
    Or getchar gives 10 value when the user presses enter key, and getch gives 13 value
    This one is probably because getch is really giving you the character as-is, rather than translating the newline sequence (i.e., in Windows: carriage return followed by new line, hence decimal 13 followed by 10 in ASCII) into the equivalent of '\n' (i.e., new line, hence 10 in ASCII).
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getchar, getch and gets inside while loop
    By sampleandhold in forum C Programming
    Replies: 13
    Last Post: 03-31-2012, 04:18 PM
  2. getch() vs. getchar()
    By mako in forum C Programming
    Replies: 5
    Last Post: 01-24-2006, 11:20 AM
  3. getch() getchar() no need 4 fflush?
    By Mikeca in forum C Programming
    Replies: 4
    Last Post: 12-03-2005, 04:16 PM
  4. Difference between getch() and getchar()
    By codec in forum C Programming
    Replies: 4
    Last Post: 04-04-2004, 02:34 AM

Tags for this Thread