getch() vs. getchar()

This is a discussion on getch() vs. getchar() within the C Programming forums, part of the General Programming Boards category; I was wondering, what the difference between these two functions is. I looked through the FAQ and couldn't find an ...

  1. #1
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69

    getch() vs. getchar()

    I was wondering, what the difference between these two functions is. I looked through the FAQ and couldn't find an explanation. I gather both read from stdin, so what's the difference?..

    Greetz,

    ~Mako

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    getch () is defined in conio.h which is non standard C, so some compilers won't have it, getchar () is in stdio.h.

    they work differently too, getch wont display any text on screen - the moment you press a key it is entered - you don't press enter. a nice way of reading in a single character without needing to flush the input buffer afterwards.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    thx. As for getch being defined in conio, I can call it just by #include ing stdio....

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    as far as i know, if its in your stdio, then your compiler is including a non standard function - i've a similar case with Sleep and stlib.h. its a compiler specific thing, and normally getch should be in conio.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Even thought getch is actually not in stdio.h, we can also use getch without including conio.h because C allows declaration-at-first-use of a function. That's, the first time you use a function, if you don't declare it before, you also declare it.

    sorry about my poor English, hope you can understand me
    Last edited by Antigloss; 01-24-2006 at 08:57 AM.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    getch() is really handy for menu programs, like so:

    Code:
    char option;
    
    printf ("Press either A, B or C.");
    option = getch();
    printf (" %d\n", option);
    
    switch (option)
    {
    case 'A':
      printf ("you pressed A!!");
      break;
    
    case 'B':
      printf ("You pressed B");
      break;
    
    case 'C':
      printf ("You pressed C");
      break
    }
    That was a very simple example, but the best thing about it is that you don't have to press the enter key afterwards. I don't think it will work for strings.

    If you want to use it for numbers, I think you have to do something like this:

    Code:
    int number;
    printf ("Enter a number: ");
    number = getch()-'0';
    This works for me. The reason why you need the -'0' thing is because it stores the ASCII value of the number, which is the number itself PLUS the value of 0 ... know what I mean?

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. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. getch() getchar() no need 4 fflush?
    By Mikeca in forum C Programming
    Replies: 4
    Last Post: 12-03-2005, 03:16 PM
  4. Difference between getch() and getchar()
    By codec in forum C Programming
    Replies: 4
    Last Post: 04-04-2004, 01:34 AM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 12:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21