Is there a delay function for C? I'm using usleep, but I just found that somewhere on the net and if I get asked about it, I won't be able to explain or even name which library I'm calling it from. Is there a delay function in time.h? If so, what is it and how do you use it?

use sleep() function which is defined in time.h. It has only a resolution of 1 millisecond however. So if you say 5000 it would be 5sec i suppose.

ssharish2005

Yes delay is necessary, because it gives time to the rest of the system and other programs, when your program doesn't need it. A delay less than 10 milliseconds (10 000 microseconds) makes no sense, and is never accurate, because the minimum amount of time which scheduler gives to a program, is 5 milliseconds. usleep is defined in unistd.h, and implements delay in microseconds. But the problem is that not every system has unistd.h, also this header is not available when we use strict standard c. Therefore it's better to use select in sys/select.h for delay, delay is implemented in that way in GTK Asteroids http://www.daniweb.com/code/snippet744.html There is also a function g_usleep in GTK glib. But when one uses mingw in windows, then the only delay which is implemented in mingw, is _sleep in stdlib.h, which again is not available in strict standard c, also g_usleep in GTK should work in windows.

Notice that none of these functions are part of the standard c, usleep and select are part of the POSIX standard, and g_usleep is part of the GTK, _sleep in stdlib.h is only the mingw addition, sleep in time.h is also a non-standard addition in a few compilers.

Please correct me if I am wrong, but I have used a function

delay(int millisecond)

The function gives delay in milliseconds. Its probably defined in dos.h, so not sure if VC will provides it and does standard C accepts it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.