繁体   English   中英

您如何在C中创建一个“等待” x秒钟的函数?

[英]How do you make a function that “waits” an x amount of seconds in C?

我试图使终端在屏幕上打印某些内容之前等待x秒钟。 我从网上从其他地方复制了代码,但是我的终端根本不等待任何时间,而是像往常一样完全执行所有操作。 你们知道为什么会这样吗?


for(int i = 0; i < 5; i++){
    delay(5);
    printf(". ");
}

void delay(int number_of_seconds)
{
    // Converting time into milli_seconds
    int milli_seconds = 1000 * number_of_seconds;

    // Stroing start time
    clock_t start_time = clock();

    // looping till required time is not acheived
    while (clock() < start_time + milli_seconds)
        ;
}

unistd.h上有一个“睡眠”功能

#include <unistd.h>

//something your code

sleep(seconds);

希望能有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM