Sleep not happening when it should


#1

I am having a little issue with the sleep() function and was hoping someone might be able to explain what’s happening.
Although the code seems fine and runs ok, when the sleep function is added it seems to do all of the sleeps first before printing the certificate text.
For example:

[code]#include <stdio.h>
#include <unistd.h>

void congratulateStudent (char *student, char *course, int numDays)

{
printf("%s has done as much %s Programming as I could fit into %d days.", student, course, numDays);
}

int main(int argc, const char * argv[])
{

congratulateStudent("Mark", "Cocoa", 5);
sleep(2);
congratulateStudent("Bo", "Objective-C", 2);
sleep(2);
congratulateStudent("Mike", "Python", 5);
sleep(2);
congratulateStudent("Ted", "iOS", 5);
return 0;

}[/code]

In the output frame it does return all the text but only after a 6 second wait. It doesn’t wait two seconds ‘between’ each line.

Also I have noticed on some other screen prints posted here there is a return between each line, whereas mine is one long sentence!
I’m not sure if it matters but I am running what appears to be Xcode 4.3.3

Any help would be much appreciated. :slight_smile:


#2

Try this to see if it works.

Add a fflush (NULL) after each congratulateStudent (…)

#include <stdio.h>
#include <unistd.h>

void congratulateStudent (char *student, char *course, int numDays)

{
    printf("%s has done as much %s Programming as I could fit into %d days.\n", student, course, numDays);
}

int main(int argc, const char * argv[])
{
    
    congratulateStudent("Mark", "Cocoa", 5);
    fflush (NULL);
    sleep(2);
    
    congratulateStudent("Bo", "Objective-C", 2);
    fflush (NULL);
    sleep(2);
    
    congratulateStudent("Mike", "Python", 5);
    fflush (NULL);
    sleep(2);
    
    congratulateStudent("Ted", "iOS", 5);
    fflush (NULL);

    return 0;
}

Also as a last resort, try running the program from the command line.


#3

That worked great! Thank you!
Do you know why I needed to add that when it seems fine for everyone else?
Also could you please explain what it did? :laughing:

Everything is still coming out as one long sentence, rather than line breaks between each one, is there a reason for that?


#4

You need a \n at the end of the printf (but inside the double quotes).

NSLog doesn’t require that end of line marker, but printf does.


#5

Your original code had only one simple problem. But first, backtrack and remove the fflush() calls that were suggested as a fix; they are not needed. Then, to the end of your formatting string in your printf call, add a \n, as in:

printf( "%s has done as much %s Programming as I could fit into %d days.\n",  <---- Append this \n

That will solve your problem completely.

Explanation: Unlike the NSLog() function, the printf() function does not automatically terminate your output string with a carriage return (i.e., the \n character); you must specify it explicitly yourself. This is actually an advantage because it enables you to construct lines of output from multiple printf() calls, which is sometimes a useful ability. Because you didn’t have a \n, all your output lines were concatenated together; that’s why you got one long output string.

The carriage return also causes the string to “flush” to the screen, so you will see the following order of events:

—display string
—sleep
—display string
—sleep
—display string

Without the carriage return, the program buffers up the string until the entire line has been constructed, so you see this order of events:

—construct part of string, but do not display it
—sleep
—construct part of string, still no display
—sleep
—construct part of string, no display
—end of program, “flush”/output string

Hope this helps.


#6

Thank you both for that, worked a treat and now I feel like i’m back on track for the moment!

Calwas, Thanks for taking the time to explain it also, I surprised myself by actually understanding your explanation.
I think it makes things much easier to take to the time to understand what your doing rather then just pop in a few bits of extra code that work but you have no idea what you’ve actually done, so this helped me greatly! :smiley: