Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Why isn't my compiler warning me about this? (http://www.velocityreviews.com/forums/t440647-why-isnt-my-compiler-warning-me-about-this.html)

Chad 12-24-2005 11:56 PM

Why isn't my compiler warning me about this?
 
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


Richard Heathfield 12-25-2005 12:16 AM

Re: Why isn't my compiler warning me about this?
 
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)

Keith Thompson 12-25-2005 12:24 AM

Re: Why isn't my compiler warning me about this?
 
"Chad" <cdalten@gmail.com> 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) kst-u@mib.org <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.

Chad 12-25-2005 02:00 AM

Re: Why isn't my compiler warning me about this?
 

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


Barry Schwarz 12-25-2005 02:42 AM

Re: Why isn't my compiler warning me about this?
 
On 24 Dec 2005 18:00:31 -0800, "Chad" <cdalten@gmail.com> 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>>

slebetman@yahoo.com 12-25-2005 09:22 AM

Re: Why isn't my compiler warning me about this?
 
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.


Emmanuel Delahaye 12-25-2005 10:44 AM

Re: Why isn't my compiler warning me about this?
 
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


All times are GMT. The time now is 05:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.