delay() mac os x

Discussion in 'Mac Programming' started by aiquecarago, Feb 4, 2008.

  1. macrumors newbie

    Joined:
    Feb 4, 2008
    #1
    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
     
  2. macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #2
    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.
     
  3. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    try sleep(1) instead.
     
  4. thread starter macrumors newbie

    Joined:
    Feb 4, 2008
    #4
    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?
     
  5. macrumors 6502

    Joined:
    Jul 25, 2006
    #5
    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);
     
  6. thread starter macrumors newbie

    Joined:
    Feb 4, 2008
    #6
    WORKS!!

    Thank you very much
     

Share This Page