Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By Salem

-Wall warnings. getchar(), sleep()

This is a discussion on -Wall warnings. getchar(), sleep() within the C Programming forums, part of the General Programming Boards category; Hey guys. So I have been learning C for a couple weeks now, and I ran into something I can't ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    32

    -Wall warnings. getchar(), sleep()

    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.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,827
    sleep() is not a standard function in C. Under unix (or more accurately posix) it is typically declared in a header named <unistd.h>. You need to #include that header.

    getchar() will give a similar warning if you forget to #include <stdio.h>
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Thanks! Why would the program still execute the sleep() function without the correct header included, though? Because it still executes the sleep function, it just gives the warning.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Well it's largely down to luck.

    The implied prototype generated by the compiler is
    int sleep();

    Which is close enough to the actual prototype of
    unsigned int sleep(unsigned int seconds);

    that it will actually work.

    Where it all falls apart is when the function in question doesn't take int parameters.
    If you try this with most of the functions in say math.h, then it will most likely produce garbage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by Salem View Post
    Well it's largely down to luck.

    The implied prototype generated by the compiler is
    int sleep();

    Which is close enough to the actual prototype of
    unsigned int sleep(unsigned int seconds);

    that it will actually work.

    Where it all falls apart is when the function in question doesn't take int parameters.
    If you try this with most of the functions in say math.h, then it will most likely produce garbage.
    Oh alright, makes sense!

    Thanks guys, I appreciate it

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Salem View Post
    If you try this with most of the functions in say math.h, then it will most likely produce garbage.
    I just tried it with 3.142/6 and received the correct answer (.5) .
    Shouldn't the integer be misinterpreted as a double without a cast and give garbage ?
    Or are the tools intelligent enough to avoid that ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > I just tried it with 3.142/6 and received the correct answer (.5) .
    I mean try calling things like sin() without a proper prototype.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Salem View Post
    > I just tried it with 3.142/6 and received the correct answer (.5) .
    I mean try calling things like sin() without a proper prototype.
    That is what I did..
    Code:
    #include<stdio.h>
    int main(void)
    {
        printf("%g",sin(3.142/6));
        return 0;
    }
    Also got that warning... (and also..if relevant.. gcc (I did not compile with g++, if you were going to say that ) does not seem to need -lm now for the math library)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    5,827
    You just got lucky. Just because something is undefined (i.e. an implementation is allowed to do anything it likes) doesn't mean you can only get erroneous output.

    If I was to use a different tool chain and library from you, and report that the output from your code was 0.7, my tool chain would still be just as correct as yours. Why? Because that is the meaning of undefined behaviour according to the C standard. There is no constraint whatsoever on what the implementation (i.e. tool chain, library, operating system) does when code exhibits undefined behaviour. But it is the code that exhibits the fault, not our respective compilers. You would probably argue it is my tool chain at fault. You would be wrong.
    rags_to_riches likes this.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by grumpy View Post
    Just because something is undefined ...
    I know that .. but isn't the fact that functions without a prototype will be considered as taking an int argument well defined ?
    3.142/6 describes a floating point number... but only if the compiler knows how it is represented ..(?right?)
    If it is called as an int...what leeway does a compiler have to correct that..without knowing what it was represented as ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Like so...
    Code:
    $ gcc bar.c
    bar.c: In function ‘main’:
    bar.c:4: warning: incompatible implicit declaration of built-in function ‘sin’
    $ ./a.out 
    0.500059
    $ gcc -fno-builtin bar.c -lm
    $ ./a.out 
    0
    manasij7479 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Camera (Wall Alpha-ing)
    By JohnLeeroy in forum Game Programming
    Replies: 3
    Last Post: 04-05-2011, 02:07 PM
  2. gcc -Wall -pedantic
    By flexo87 in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 01:04 PM
  3. Hit a brick wall here...
    By jwillisoa in forum C Programming
    Replies: 2
    Last Post: 07-15-2007, 09:14 PM
  4. The wall behind is showing over the wall in front. (Glut)
    By Queatrix in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2005, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21