I am following the book "C Primer Plus" and encounter such a snippet of code:
#include <stdio.h>
#include <ctype.h>
#define SPACE ' '
int main(void) {
char ch = getchar();
while (ch != '\n')
{
if (isalpha(ch))
putchar(ch + 1); //change other characters
else
putchar(ch);
ch = getchar();
}
putchar(ch); //print the newline
return 0;
}
run it and output:
$ ./a.out
a c programmer
b d qsphsbnnfs
I assume when I input a
, it will immediately output b. However, it wait until I strike enter.
It seem that the second putchar(ch) works properly.
What's the reason putchar(n+1)
not put it char immediately as the second putchar(ch) did?