Implicit declaration of function 'sleep'


#1

Hi guys,

I have run into my first problem that I cant solve.

the error message is “Implicit declaration of function ‘sleep’ is invalid in C99”

[code]//
// main.c
// ClassCertifficates
//
// Created by Zeak Smith on 15/12/12.
// Copyright © 2012 Zeak Smith. All rights reserved.
//

#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]

can anyone point out where I am going wrong?

thanks in advance
Zeak


#2

The error message means the function is being used but the complier has not seen a declaration for it.

You must declare the function before using it:

// main.c

// Must declare before using it
unsigned int sleep (unsigned int seconds);

// Or delete the above declaration and uncomment this include directive
// #include <unistd.h>  // for sleep

void congratulateStudent (char *student, char *course, int numDays)
{
    ...
}

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

#3

Thanks for the solution. The book could’ve been much clearer here, as it seems this simply will not work as described by the author. However, I guess it’s also a lesson in researching elsewhere for the answer, so not necessarily a bad thing!

Thanks again
James


#4

Hi, quick question here, I get the implicit declaration when I declare #include <stdlib.h> as instructed in the book.

However, when I use #include<unistd.h> instead, the program works fine.

Please advise why this is so. Thank you.


#5

Read the previous posts or the manual pages to find out where the prototype for the sleep function is declared (do man 3 sleep from the command line to bring up the manual pages.)


#6

Thanks, I got it.