S
Stanley Rice
Dear all
I got a question about the key word 'extern', In f1.c, I define a
variable as below:
--------------f1.c--------------
float var = 3.0F;
but in file main.c, I declare it as type double
---------------main.c-------------
extern double var;
......
printf("%f\n", var);
.....
At first, I guess that the compiler(gcc) will shout to me, as 'var' in
f1.c is defined in type float, but I declare the var in main.c with
type double, which is inconsistant. However, the compiler happily
accept it. But the result isn't what I expect. It prints 0.000000
instead of 3.0, and I couldn't find the reason here.
For another try, I define a variable as below:
--------------f2.c------------------
unsigned int var_int = 4;
in another file, say, main.c, I declare it as below
-------------main.c----------------
extern int var_int;
.....
printf("%d\n", var_int);
This time, define var_int in type 'unsigned int' but declare it in
type 'int' in another file, the printed result is 4, which is
consistant with what it is previously defined.
Could anyone what happens? why doesn't the compiler complains, and why
the two results differs. Thanks in advance.
I got a question about the key word 'extern', In f1.c, I define a
variable as below:
--------------f1.c--------------
float var = 3.0F;
but in file main.c, I declare it as type double
---------------main.c-------------
extern double var;
......
printf("%f\n", var);
.....
At first, I guess that the compiler(gcc) will shout to me, as 'var' in
f1.c is defined in type float, but I declare the var in main.c with
type double, which is inconsistant. However, the compiler happily
accept it. But the result isn't what I expect. It prints 0.000000
instead of 3.0, and I couldn't find the reason here.
For another try, I define a variable as below:
--------------f2.c------------------
unsigned int var_int = 4;
in another file, say, main.c, I declare it as below
-------------main.c----------------
extern int var_int;
.....
printf("%d\n", var_int);
This time, define var_int in type 'unsigned int' but declare it in
type 'int' in another file, the printed result is 4, which is
consistant with what it is previously defined.
Could anyone what happens? why doesn't the compiler complains, and why
the two results differs. Thanks in advance.