开发者

Program to cause 1 sec delay in C and than print stuff

example sec 1 ---

sec开发者_如何学Python 2 ---

sec 3 ---

Each print should have a delay of 1 sec.


In the absense of any other information in your question...

You should find a sleep function in nearly any C environment (note that it's all lower case). It's usually in time.h or unistd.h, and accepts the number of seconds that it should delay execution.

Many environments will also have nanosleep, which is an equivalent that accepts a number of nanoseconds rather than seconds. Also in time.h on many systems.

Bottom-line is that your C environment is likely to provide such a function, whether it's sleep, _sleep, Sleep, or something else similar, and whether it accepts seconds, nanoseconds, or milliseconds. You'll have to refer to the documentation for your environment to find the specific one.


#include <windows.h>

...

Sleep(timeInMilliseconds); //sleeps the current thread

hth


Unfortunately there isn't a portable version of sleep() so instead you could write a delay() function using the standard functions in time.h as follows:

void delay(int seconds)
{
    time_t t = time(NULL);
    while (difftime(time(NULL), t) < seconds) ;
}

Note that this isn't ideal as it keeps the cpu busy during the delay.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜