Thread: Difference between getch() and getchar()

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    Difference between getch() and getchar()

    Hi,

    I was recently porting a Windows app to FreeBSD, and I realised that the getch() function is not available in UNIX.

    In the FAQ board it says that there is no UNIX equivalent to getch() from conio.h and gives a code snippet to use instead.

    I succesfully replaced all occurences of getch() with getchar() from stdio.h with no problems.

    I'm sure there must be a difference, otherwise kermi3 wouldn't have said what he did. Can anyone tell me the difference, and why getchar() is not a direct equivalent of getch()?

    http://cboard.cprogramming.com/showthread.php?t=27714

    Thanks,

    Daniel

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    getch() reads a key hit without waiting for you to press enter.
    getchar() requires you to hit enter.
    There is no ANSI standard method for reading a keypress, because there can't be. C is about a language, not specific hardware.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    17
    Hi,
    I am not an expert on this, but I had a problem with this in the past. I know the main difference between getche() and getchar() is that with getche() you don't need to press Enter after entering a character. So, as a student, in my opinion, I just use getche() as a convenience feature. Interested what others have to say on this too.

    Yev

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    getche() echos also, where as getch is not. Points of reference for the interested:

    getch
    getche

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    theres a getch() if you have ncurses installed. (on linux)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  3. getch() vs. getchar()
    By mako in forum C Programming
    Replies: 5
    Last Post: 01-24-2006, 11:20 AM
  4. getch() getchar() no need 4 fflush?
    By Mikeca in forum C Programming
    Replies: 4
    Last Post: 12-03-2005, 04:16 PM
  5. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM