开发者

looking for a function that doesn't do anything/ waits

for C, is there a function that takes an开发者_开发百科 int and doesn't execute next statement ?

printf("a");
wait(500);
printf("b");

b will be printed after 500ms after a is printed out. something of the sort. sorry for the stupid question but i wasn't sure how to go about searching for such function.


There is nothing like that in standard C. However, POSIX defines the sleep() function (which takes an argument in seconds), usleep() (which takes an argument in microseconds), and nanosleep() (nanosecond resolution).

It is also possible to use the select() function with NULL for all three file descriptor sets to sleep for sub-second periods, on older systems which don't have usleep() or nanosleep() (this is not so much a concern these days).


I believe you are looking for the sleep() or usleep() functions.


On Windows you could try:

#include <windows.h>

int main()
{
    // Do nothing for 5 seconds...
    Sleep(5000);

    return 0;
}

Read more about this, here (official doc) or here (linux too).


Take a look at this if you're on *nix.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜