简体   繁体   中英

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).

我相信您正在寻找sleep()或usleep()函数。

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) .

看看这个 ,如果你在* nix。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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