0

I use DevCpp, just curious and want to know why this 2 is different,

char ch_1=getchar ();
printf ("getchar: %d\n", ch_1);
char ch_2=getch ();
printf ("getch: %d\n", ch_2);

I pressed "Enter" 2 times


getchar: 10
getch: 13

It produced 10 and 13, which means getchar produces 10 (ascii of \n) and getch produces 13 (ascii of \r). Why is it different? Why not both 10 or both 13? Why getch produces carriage return while getchar produces new line?

  • try to press "enter" only one time – Iłya Bursov Apr 28 '20 at 19:55
  • @IłyaBursov It will wait for an input for getch... – LastSecond959 Apr 28 '20 at 19:56
  • @usr2564301 You mean `getch` first and then `getchar`? It prints out 13 and 10. – LastSecond959 Apr 28 '20 at 19:58
  • 2
    `getch` is a non-standard Microsoft function that bypasses buffering. Why do you want to use it? – Tom Karzes Apr 28 '20 at 20:00
  • @TomKarzes What do you mean by **bypasses buffering**? My English is not that good so I don't understand well... – LastSecond959 Apr 28 '20 at 20:01
  • 1
    [off-topic] best way to learn C is to ignore Microsoft. – wildplasser Apr 28 '20 at 20:05
  • 1
    @LastSecond959 The C standard I/O library supports input and output buffering. This means that data is saved in a buffer and only sent when the buffer is full, or when it needs to be sent. In contrast, unbuffered I/O always occurs immediately, and in general is much less efficient because it can make far more system calls. You want to avoid unbuffered I/O as much as possible. And you also want to avoid mixing buffered and unbuffered I/O for a given stream. – Tom Karzes Apr 28 '20 at 20:05

0 Answers0