Advertisement
paulogp

getchar problem

Jul 25th, 2011
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 KB | None | 0 0
  1. // apple xcode
  2. // paulogp
  3.  
  4. /* getchar problem */
  5. /* Despite what it says in the brochure, getchar() does not read a
  6.  single char­ acter from the keyboard. That's what the getchar()
  7.  function returns, but it's not what the function does.
  8.  Internally, getchar() reads what's called standard input. It grabs
  9.  the first char­acter you type and stores it, but afterward it sits
  10.  and waits for you to type something that signals "the end." */
  11.  
  12. #include <stdio.h>
  13.  
  14. int main (int argc, const char * argv[])
  15. {
  16.     char the_char_a, the_char_b;
  17.    
  18.     printf("caracter a: ");
  19.     the_char_a = getchar();
  20.     fpurge(stdin); // fflush(stdin);
  21.    
  22.     printf("caracter b: ");
  23.     the_char_b = getchar();
  24.    
  25.     printf("a: %c; b: %c\n", the_char_a, the_char_b);
  26.    
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement