I study the behavior of global variables.
So far, I have thought that pluralizing global variables is an illegal way and should receive an error message. But I got an unexpected result from the Borland C / C ++ compiler, while GCC gave me the expected result.
the code:
test1.c :
#include<stdio.h> void func(void); int num=1; void main(){ func(); return; }
test2.c :
#include<stdio.h> int num=2; void func(){ printf("%d",num); return; }
At the MS-DOS prompt
results
There are no errors and compiling & link successfully (this is unexpected for me). After running test1.exe the console printed 2. This is the num value defined in test2.c .
GCC gave me an error with several num definitions. Of course a.exe not done (this is what I expected)
Why is this happening? Please let me know. Thanks!
source share