c++ - printf() 函数在 time() 函数之后执行?C/C++
问题描述
我写了以下程序:
#include <cstdio>
#include <ctime>
int main()
{
time_t t;
int d;
printf("\nHello 1");
time(&t);
d=t;
while (t-d<3) /*wait 3 seconds*/
{
time(&t);
}
printf("\nHello 2");
time(&t);
d=t;
while (t-d<3) /*wait 3 seconds*/
{
time(&t);
}
printf("\nHello 3");
return 0;
}
构建程序后,我运行它。程序等待 3 秒以显示“Hello 1”行。
为什么先写后执行?
解决方案
在某些操作系统上,输出缓冲区stdout
通常需要“刷新”才能使您在那里打印的内容出现在屏幕上。\n
充当“冲洗”并解释了行为。
您可以通过fflush(stdout);
在每个printf
. 这会强制进行屏幕更新。或者将 移动\n
到每行的末尾而不是开头。
推荐阅读
- c# - 如何从 Xamarin 表单中的集合视图触发 pin 单击事件
- python - 通过python中的多个图连续着色
- apostrophe-cms - 如何使用撇号工作流在 nunjucks 文件中获取撇号中的当前语言环境
- database - 如何使用 Orca 将 DolphinDB 的 DFS 表加载到 Python 客户端?
- flutter - setState 依赖于另一个类的值
- python - 在彩色框中裁剪图像
- powershell - Powershell - 从集合中删除重复项
- excel - 如何通过 PowerShell 脚本将现有 csv 文件添加为现有 excel 工作簿中的新工作表
- react-native - 如何在 react-native 中禁用某些页面中的后退按钮?
- javascript - 如何从 JavaScript 中的数组创建 JSON 文件?