Thread: getch() where is it getting from? k&r p.136

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

    getch() where is it getting from? k&r p.136

    Greetings,

    int c, getch(void);
    while (isspace(c = getch()))
    ;

    getch() where is it getting a character from?
    I checked The GNU Operating System but no getch (plenty of getchar).


    Also where can I view a "C" programmers reference guide?

    Thanks...vmars316
    Win7x64, HotBasic, 'Beginning C programming', 'Arduino Uno R3'.
    "All things in moderation, except for love and forgiveness"... www.vmars316.com

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    getch() gets character from stdin, is that what you mean?

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    31
    Quote Originally Posted by camel-man View Post
    getch() gets character from stdin, is that what you mean?
    Yes, that's it, Thanks...vm
    Win7x64, HotBasic, 'Beginning C programming', 'Arduino Uno R3'.
    "All things in moderation, except for love and forgiveness"... www.vmars316.com

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    There's a reason it isn't documented: it's only available on old Windows machines.

    Getchar() is the standard equivalent present in every C implementation. Note: You have to press enter to make getchar() work, and it leaves the newline character in the buffer.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    True, it's not standard C, but it's not only valid on old Windows (or DOS) machines, i.e. it's not just a legacy from Turbo C. It also exists as part of the ncurses library, which is still in use in the *nix world. Since it's not standard, we can't say for sure what it reads from without seeing the definition. But my guess is that it's defined elsewhere in the book. I don't have my K&R book handy, but IIRC from other threads here, it's a function that they wrote along with an ungetch. It is used to buffer keyboard input, in case your program read too far and needs to "unread" or "put stuff back in stdin". The ungetch puts it back in a stack (implemented as an array) so you can safely retrieve it later, as though you never read too far and it was still in the standard input buffer. It's used, as part of their RPN calculator example program, and maybe elsewhere.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    The getch() that matters on page 136 in section 6.3 of K&R 2nd Ed.[1] is the one that is shown on page 79 in section 4.3. So compiler extensions do not matter.

    By the way, that getch() calls getchar() which, as already mentioned earlier in the thread, gets its input from stdin (also known as standard input).

    Is it really dangerous to use the verb "gets" in that last sentence or is that only when I use the function gets()?

    [1] Kernighan, Brian W. and Dennis M. Ritchie, The C Programming Language, Second Edition, Murray Hill, New Jersey: AT&T Bell Laboratories, 1988.
    Last edited by pheininger; 03-08-2012 at 06:14 AM. Reason: Add the line about gets. Then added full reference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getch Refresh? Simple Problems with Getch()
    By justin777 in forum C Programming
    Replies: 7
    Last Post: 10-26-2011, 01:21 AM
  2. getch() ??
    By kalamram in forum C Programming
    Replies: 23
    Last Post: 06-23-2006, 10:16 PM
  3. getch
    By KevBin in forum C Programming
    Replies: 1
    Last Post: 11-20-2004, 09:11 AM
  4. getch()
    By Stan100 in forum C++ Programming
    Replies: 7
    Last Post: 12-09-2002, 02:53 PM
  5. getch() ?
    By HELP in forum C Programming
    Replies: 2
    Last Post: 11-06-2002, 03:07 AM

Tags for this Thread