Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aiquecarago

macrumors newbie
Original poster
Feb 4, 2008
4
0
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

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.
 

aiquecarago

macrumors newbie
Original poster
Feb 4, 2008
4
0
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

macrumors 6502
Jul 25, 2006
294
0
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);
 

Nurmuhammad

macrumors newbie
Nov 5, 2023
1
0
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);
Works but it comes under i want to put them alongside
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.