printf除非换行符在格式字符串中,为什么在调用后不刷新?这是POSIX的行为吗?我怎么可能printf每次都立即冲洗?
我知道大多数终端默认处于行缓冲模式.即输出被缓冲,并且在遇到换行字符之前不会被引导到stdout.
所以我希望它什么都不打印(至少在填充缓冲区之前):
int main() {
while(1) {
printf("Haha");
sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它确实在很短的时间内打印出来.
如果我想每秒打印"哈哈",我可以在printf之后printf("Haha\n")或者fflush(stdout)之后做.(我知道这不是那么便携,但它仍然是一个解决方案)
现在我回想起非常经典的scanf程序(我添加了while(1)循环以防止程序退出时缓冲区刷新):
int main() {
char char_in;
while(1) {
printf("Haha. Input sth here: ");
scanf("%c", &char_in);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在程序打印Haha. Input sth here:(并等待我的输入).如果我删除scanf语句,它不在这里.为什么会这样?
谢谢.
我在window xp上使用emacs23.4。所以shell模式使用windows cmd.exe。
\n\n我遇到这样的问题,如果我使用要求用户输入\xef\xbc\x88.eg电子邮件、密码\xef\xbc\x89的命令,我的emacs不会显示该请求。
\n\n我已经注释掉了 .emacs 中的所有配置。
\n\n图为我在emacs和cmd中输入“heroku login”的结果。
\n\n
所以我是C的新手.我正在使用带有MinGW编译器的eclipse.我在第二章使用scanf和printf函数,我的程序正在运行,但只有在我将三个int输入scanf函数后才将语句打印到控制台.
#include <stdio.h>
int main(void){
int length, height, width, volume, dweight;
printf("Enter the box length: ");
scanf("%d", &length);
printf("\nEnter the box width: ");
scanf("%d", &width);
printf("\nEnter the box height");
scanf("%d", &height);
volume = length * width * height;
dweight = (volume + 165) / 166;
printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
printf("Volume: %d\n", volume);
printf("Dimensional Width: %d\n", dweight);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
8 (user input + "Enter" + key)
10 (user input + …Run Code Online (Sandbox Code Playgroud)