tags:

views:

109

answers:

4

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.

+1  A: 

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

shinkou
+8  A: 

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

caf
If you want Windows, it has the function "sleep()" which takes microseconds.
Grant Peters
Grant Peters: milliseconds, I believe.
caf
The Windows API has `Sleep()` (capital S), which takes milliseconds: http://msdn.microsoft.com/en-us/library/ms686298%28VS.85%29.aspx
Josh Townzen
thank you, that's exactly what i needed!
Fantastic Fourier
+3  A: 

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

Ants
+3  A: 

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

Wilhelm