PDA

View Full Version : delay() mac os x




aiquecarago
Feb 4, 2008, 01:26 PM
Hello

I started a few months ago learning how to program in C with xCode.

Now i have to make a program that uses functions like delay(), but can't figure out how to do it in mac os X.

for example:

main(){

printf("Hello");

delay(1000); //have to wait 1 second to print world

printf("World");
}

Any help?
Thanks in advance



yeroen
Feb 4, 2008, 01:39 PM
Use the sleep() function from the standard C library. The prototype is:

#include <unistd.h>

unsigned int sleep(unsigned int seconds)


For more info, do a man on sleep. There are other variants like usleep, nanosleep, etc.

Eraserhead
Feb 4, 2008, 01:40 PM
try sleep(1) instead.

aiquecarago
Feb 4, 2008, 02:12 PM
Already tried it, but not working:

with sleep and other functions like sleep when i run the program he first wait 1 second and then print the two printf's, and i want the program to print the first printf, wait a second and then print the second printf. It's weird. Any help?

kpua
Feb 4, 2008, 02:50 PM
The output stream is buffered up to newlines. You either need to put a "\n" at the end of your strings or call this after (at least) the first printf:
fflush(stdout);

aiquecarago
Feb 4, 2008, 03:07 PM
WORKS!!

Thank you very much