By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Why isn't my compiler warning me about this?

P: n/a
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

Dec 25 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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)
Dec 25 '05 #2

P: n/a
"Chad" <cd*****@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) ks***@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.
Dec 25 '05 #3

P: n/a

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

Dec 25 '05 #4

P: n/a
On 24 Dec 2005 18:00:31 -0800, "Chad" <cd*****@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>>
Dec 25 '05 #5

P: n/a
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 '05 #6

P: n/a
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
Dec 25 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.