Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why isn't my compiler warning me about this?

Reply
Thread Tools

Why isn't my compiler warning me about this?

 
 
Chad
Guest
Posts: n/a
 
      12-24-2005
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

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-25-2005
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)
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-25-2005
"Chad" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      12-25-2005

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

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-25-2005
On 24 Dec 2005 18:00:31 -0800, "Chad" <(E-Mail Removed)> 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>>
 
Reply With Quote
 
slebetman@yahoo.com
Guest
Posts: n/a
 
      12-25-2005
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.

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      12-25-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why no compiler-warning at invalid array-initializing ? carlos@gkpwdun.com Java 11 11-07-2005 08:34 PM
Why the compiler gives warning ? junky_fellow@yahoo.co.in C Programming 13 08-10-2005 06:06 AM
Why compiler not generating any warning ? junky_fellow@yahoo.co.in C Programming 29 06-11-2005 10:18 PM



Advertisments