Redefining Global in C! 🤔🤯
This post explores how redefinition of a symbol may be possible when using the C language.
Redefining a global variable is not allowed in C. For the most part this statement is right. Compiling the following will fail because a
is redefined as a float
, after having defined as an int
.
Compiler throws the following error -
The compiler complains about redefinition of a
. So, a
cannot be redefined? 😏
Try this instead!
Move declaration of a
as an int
in another .c
file. And retain the main.c
as below -
Compile both the files.
❯ gcc external.c main.c -o main
main.c:6:30: warning: format specifies type 'int' but the argument has type 'float' [-Wformat]
printf("int? - a = %d\n", a);
~~ ^
%f
1 warning generated.
warning
? No error? Yes. It generated the binary. 😱