【发布时间】:2021-10-20 17:45:27
【问题描述】:
#include <stdio.h>
int main(){
char a[2] = {0};
a[0] = 't';
printf("%s", a);
scanf("%c", a);
return 0;
}
scanf 这里会导致标准输出的自动刷新。
运行a.out会在运行scanf之前在终端打印t,更多信息:How is printf getting flushed before scanf is executed?
但是,执行a.out > out.txt 并使用^C 终止它不会在out.txt 中打印任何内容,但输出仍会出现在屏幕上,而不会使用> 重定向标准输出。
如果正在刷新标准输出,那么为什么out.txt 仍然是空的?
如果它没有被刷新,那么t 在第一个示例中是如何出现在屏幕上的?
(我知道使用 \n 或手动 fflush 或正确终止程序将解决问题,我只是对这种行为感到好奇)。
【问题讨论】:
标签: c