Why isn't my compiler warning me about this?

C

Chad

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;
}
 
R

Richard Heathfield

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.
 
K

Keith Thompson

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. 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.
 
C

Chad

Richard said:
Chad said:


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
 
B

Barry Schwarz

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>>
 
S

slebetman

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. 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;
}

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.
 
E

Emmanuel Delahaye

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top