Why isn't my compiler warning me about this?

Discussion in 'C Programming' started by Chad, Dec 24, 2005.

  1. Chad

    Chad Guest

    In the following code snippet, I declare the variable val as float in
    the calc.h header file, and define it
    as int in test.c. The question is, when I compile this with full
    warnings, I get no errors about the difference between float and int.
    What did I miss here?

    -------------calc.h---------------------------------
    #ifndef CALC_H
    #define CALC_H

    extern float val;

    #endif
    ----------------------------------------------------

    ---------------test.c----------------------------------
    include <stdio.h>
    #include "calc.h"

    int main(void){
    int val=100;
    printf("The value is: %d\n", val);
    return 0;
    }
    --------------------------------------------------

    Thanks in advance
    Chad
     
    Chad, Dec 24, 2005
    #1
    1. Advertising

  2. Chad said:

    > In the following code snippet, I declare the variable val as float in
    > the calc.h header file, and define it
    > as int in test.c.


    No, you don't define it as an int in test.c. You define a local object in
    main by that name and with that type, but you haven't defined the val
    object that you declared in calc.h.

    > The question is, when I compile this with full
    > warnings, I get no errors about the difference between float and int.
    > What did I miss here?


    Scope.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 25, 2005
    #2
    1. Advertising

  3. "Chad" <> writes:
    > In the following code snippet, I declare the variable val as float in
    > the calc.h header file, and define it
    > as int in test.c. The question is, when I compile this with full
    > warnings, I get no errors about the difference between float and int.
    > What did I miss here?
    >
    > -------------calc.h---------------------------------
    > #ifndef CALC_H
    > #define CALC_H
    >
    > extern float val;
    >
    > #endif
    > ----------------------------------------------------
    >
    > ---------------test.c----------------------------------
    > include <stdio.h>
    > #include "calc.h"
    >
    > int main(void){
    > int val=100;
    > printf("The value is: %d\n", val);
    > return 0;
    > }
    > --------------------------------------------------


    There's no reason you should get a warning about float vs. int.
    For example, the following:

    #include <stdio.h>
    float val;
    int main(void)
    {
    int val = 42;
    printf("val = %d\n", val);
    {
    char val = 'x';
    printf("val = '%c'\n", val);
    }
    return 0;
    }

    produces the following output:

    val = 42
    val = 'x'

    The int val is at block scope, so it hides the global float val. The
    char varl is also at block scope, so it hides the int val in the
    enclosing scope. These are perfectly ordinary scoping rules; there's
    no need for a warning.

    On the other hand, a warning might be appropriate for declaring
    "extern float val;" and not actually defining the object. Apparently
    you're not getting a warning because you don't actually refer to the
    object (and the fact that the object you do refer to has the same name
    is irrelevant). I'm not sure what the rules are in this area, but I'm
    sure others know them.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 25, 2005
    #3
  4. Chad

    Chad Guest

    Richard Heathfield wrote:
    > Chad said:
    >
    > > In the following code snippet, I declare the variable val as float in
    > > the calc.h header file, and define it
    > > as int in test.c.

    >
    > No, you don't define it as an int in test.c. You define a local object in
    > main by that name and with that type, but you haven't defined the val
    > object that you declared in calc.h.
    >


    Okay, let me get this right. There is a difference between defining a
    local object in main vs actually defining the val object in this
    example?

    Chad
     
    Chad, Dec 25, 2005
    #4
  5. On 24 Dec 2005 18:00:31 -0800, "Chad" <> wrote:

    >
    >Richard Heathfield wrote:
    >> Chad said:
    >>
    >> > In the following code snippet, I declare the variable val as float in
    >> > the calc.h header file, and define it
    >> > as int in test.c.

    >>
    >> No, you don't define it as an int in test.c. You define a local object in
    >> main by that name and with that type, but you haven't defined the val
    >> object that you declared in calc.h.
    >>

    >
    >Okay, let me get this right. There is a difference between defining a
    >local object in main vs actually defining the val object in this
    >example?
    >

    No, in your example you don't define the global val object. You only
    declare it. If you remove the extern from the declaration (not
    recommended since objects should not be defined in headers, only
    declared) it would still not be a problem. This is because the
    function scope object val hides the file scope object. From within
    main the global variable would be invisible.

    The fact that is in a header is irrelevant. The same would be true
    for code like

    float x;
    int main(void){
    int x;
    .....
    }

    Look up scope in your handy-dandy reference of choice.


    <<Remove the del for email>>
     
    Barry Schwarz, Dec 25, 2005
    #5
  6. Chad

    Guest

    Chad wrote:
    > In the following code snippet, I declare the variable val as float in
    > the calc.h header file, and define it
    > as int in test.c. The question is, when I compile this with full
    > warnings, I get no errors about the difference between float and int.
    > What did I miss here?
    >
    > -------------calc.h---------------------------------
    > #ifndef CALC_H
    > #define CALC_H
    >
    > extern float val;
    >
    > #endif
    > ----------------------------------------------------
    >
    > ---------------test.c----------------------------------
    > include <stdio.h>
    > #include "calc.h"
    >
    > int main(void){
    > int val=100;
    > printf("The value is: %d\n", val);
    > return 0;
    > }
    > --------------------------------------------------
    >
    > Thanks in advance
    > Chad


    Others have pointed out about file scope and it seems you understand
    it. Others have also pointed out the fact that you didn't actually
    define a val of type float. And it seems you don't get it. To
    understand this, modify your code to the following and try to compile:

    -------------calc.h---------------------------------
    #ifndef CALC_H
    #define CALC_H

    extern float val;

    #endif
    ----------------------------------------------------

    ---------------test.c----------------------------------
    include <stdio.h>
    #include "calc.h"

    int main(void){
    printf("The value is: %f\n", val);
    return 0;
    }
    --------------------------------------------------

    you should get an error message. If not from the compiler then from the
    linker.
     
    , Dec 25, 2005
    #6
  7. Chad a écrit :
    > In the following code snippet, I declare the variable val as float in
    > the calc.h header file, and define it
    > as int in test.c. The question is, when I compile this with full
    > warnings, I get no errors about the difference between float and int.
    > What did I miss here?
    >
    > -------------calc.h---------------------------------
    > #ifndef CALC_H
    > #define CALC_H
    >
    > extern float val;
    >
    > #endif
    > ----------------------------------------------------
    >
    > ---------------test.c----------------------------------
    > include <stdio.h>
    > #include "calc.h"
    >
    > int main(void){
    > int val=100;
    > printf("The value is: %d\n", val);
    > return 0;
    > }
    > --------------------------------------------------


    They are different objects. 'int val' is a local variable of main()
    'shadowing' the global 'float val'.

    My compiler has warned me about that:

    Compiling: main.c
    main.c: In function `main_':
    main.c:11: warning: declaration of 'val' shadows a global declaration
    main.c:6: warning: shadowed declaration is here


    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Dec 25, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,138
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    979
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,223
    Smokey Grindel
    Dec 2, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    610
    PerlFAQ Server
    Feb 11, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    601
    PerlFAQ Server
    Mar 9, 2011
Loading...

Share This Page