Hey guys. So I have been learning C for a couple weeks now, and I ran into something I can't quite figure out. I am using gcc 4.6.1 on Linux to compile my programs. I have also been using the -Wall flag. This program is very basic and rudimentary, I know that, I am just learning about file i/o so I just wrote this to test.
Code:
#include <stdio.h>
#include <stdlib.h>
void read(void);
char fname[10];
char lname[20];
int main(void)
{
FILE *write;
write=fopen("name.mdl", "w");
if (NULL==write){
printf("File could not be created\nPlease create the file then restart program");
}
else{
printf("Enter your first and last name\n:");
scanf("%s%s", fname, lname);
fprintf(write, "%s\t%s", fname, lname);
}
fclose(write);
printf("Now I will try and open the file and read the name you entered\n");
sleep(3);
read();
return 0;
}
void read(void)
{
FILE *read;
read=fopen("name.mdl", "r");
if (NULL==read){
printf("The file could not be opned\n");
}
else{
fscanf(read, "%s%s", fname, lname);
printf("\nYou entered\n\n:%s %s\n\n", fname, lname);
printf("\n\n\tTADAAAAAA!!!\n");
sleep(4);
}
return;
}
So, in this program, running on Linux, the sleep() command will tell the terminal to sleep with the argument in seconds (4). When I compile without the -Wall flag, it compiles, runs, etc just fine. However, when I use the -Wall flag, I get
Code:
$ gcc -Wall files.c -o filetest1
files.c: In function ‘main’:
files.c:29:5: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
The code is still compiled, and produces the executable, but I get the compiler warnings.
The same thing happens when I use something like getchar() or the like. Is there something I can do to get rid of these warnings?
Thanks for your help.