Sleep() not working correctly


#1

Hi,

When I execute this:

[code]#include <stdio.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", "Cocos", 5);
sleep(2);
congratulateStudent("Bo", "Objective-C", 2);
sleep(2);
congratulateStudent("Mike", "Python", 5);
sleep(2);
congratulateStudent("Ted", "iOS", 5);
return 0;
[/code]

There is a 2 second pause between. But when I remove two of the arguments like this:

[code]#include <stdio.h>
#include <unistd.h>
void congratulateStudent(char *student)
{
printf("%s",
student);
}

int main(int argc, const char * argv[])
{
congratulateStudent(“Mark”);
sleep(2);
congratulateStudent(“Bo”);
sleep(2);
congratulateStudent(“Mike”);
sleep(2);
congratulateStudent(“Ted”);
return 0;

}
[/code]
it takes 6 seconds before it executes all of them. Why is that so? I only removed two arguments, why do it wait for 6 seconds total and execute all of them at the same time, instead of doing the same as with 3 arguments?


#2

That’s probably because the printf in the second version does not print the new line character unlike the one in the first version.

Change this:

void congratulateStudent (char *student)
{
    printf ("%s", student);
}

To:

void congratulateStudent (char *student)
{
    printf ("%s\n", student);
}

And try again. ("\n" in printf’s format string flushes the output stream as well as inserting a new line character.)


#3

Thank you! : )


#4

Running into a issue with the sleep(2); exercise. Newby question.

When I add the sleep [function] to the code I get the expected warning about the “implicit declaration”. Question how do I ignore it when I run the app? When I run the app I get a message about build failed (sleep is invalid in C99). Not a big deal I am sure that I can move on but curious how to go about ignoring the warning to complete the build of the app.

Thanks


#include <stdio.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);
    sleep(2);
    congratulateStudent("Bo", "Objective-C", 2);
    sleep(2);
    congratulateStudent("Mike", "Python", 5);
    sleep(2);
    congratulateStudent("Ted", "iOS", 5);


     
    return 0;
}


#5

Ignoring the warning is not cool; you should learn how to fix it :slight_smile:

You can add its prototype (or signature) before you first reference the sleep function:

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

    congratulateStudent ("Mark", "Cocoa", 5);
    sleep (2);
    ...
}

Or you can alternatively include the header file that declares the function:

#include <unistd.h>

int main (int argc, const char * argv[])
{
    congratulateStudent ("Mark", "Cocoa", 5);
    sleep (2);
    ...
}

#6

Thank you. I will give this a try.


#7

ibex10 - I was wondering how you knew that unistd.h had that function? In the OOC book it indicates to use stdlib.h.

Is there some way to search for a particular function in standard libraries? (I searched for ‘sleep’ in Xcode documentation but it didn’t come up)

Thanks for your help.


#8

It is in the manual pages.

You can display the manual page for the sleep function by running the following command:

man 3 sleep

Start the Terminal App and enter: man 3 sleep.

Note: 3 designates the section number of the page.

If this does not work, install the Command Line Tools: Xcode Preferences… Downloads

Also, if you want to look up something don’t quite know where to start you can invoke the apropos command.

Where is that random number function?

apropos random

apropos random command will produce something like this:

From there you can find the section number.

For example: man 3 arc4random_uniform


#9

ibex10,

Thank you so much for your comprehensive response. man 3 sleep does indeed show the exact library:

SLEEP(3)                 BSD Library Functions Manual                 SLEEP(3)

NAME
     sleep -- suspend thread execution for an interval measured in seconds

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <unistd.h>

For anyone that is looking at this thread - you can find a listing of the manual sections here:
http://en.wikipedia.org/wiki/Man_page#Manual_sections

Once again, thanks so much for your help.