使程序在C中等待特定时间

时间:2016-05-11 20:43:06

标签: c

我想要的代码是: 印刷“你好”, 等待2秒,然后 印刷“世界”。

会发生什么:     一旦我运行程序,它等待两秒钟,然后打印“helloWorld”

#include <stdio.h>
#include <time.h>

int main(void)
{

time_t current_time = 0; //A variable to save the time

printf("Hello");

current_time = clock();
//wating 2 seconds
for ( ; (clock() - current_time) < (2 * CLOCKS_PER_SEC); );

printf("Wolrd");
return 0;
}

好像wait命令在两个打印命令之前?!

1 个答案:

答案 0 :(得分:1)

打印一个新行以刷新标准输出:

 printf("Hello\n");

目前,标准输出中不会显示任何字符,因为它是行缓冲的。您也可以调用fflush函数获得相同的结果。