我正在阅读"C Primer Plus“一书,并遇到这样一段代码:
#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;
}运行它并输出:
$ ./a.out
a c programmer
b d qsphsbnnfs我假设当我输入a时,它会立即输出b。但是,它一直等到我按回车。
第二个putchar(ch)似乎工作正常。
putchar(n+1)为什么不像第二个putchar(ch)那样立即使用char呢?
发布于 2018-10-16 01:44:40
输出缓冲C库stdio有新行的缓冲区有输出缓冲。禁用缓冲调用setbuf(stdout, NULL);
https://stackoverflow.com/questions/52826810
复制相似问题