Don't talk about anything. paste the code first. Enter the following code in vs2010 (the Mac Book is out of power (I don't want to find the power source, but cannot test it in Mac OS xcode ))
//A.c fileintiValue;
//B.c fileintiValue;
//main.c file#include <stdio.h>extern intiValue;int main(){ printf("%d\n", iValue);}
The C language has the following standards:
When a variable is not initialized during peripheral declaration, the variable cannot be defined. For example: int ivalue; this code is not defined and can only be declared.
According to the C standard, the above. C and B. the ivalue in C can only be declared. Therefore, an error should be reported when the program is linked (the definition of ivalue is not found). However, the test result on vs indicates that the program runs successfully, and the output result is 0.
Compared to the definition of a variable declaration without initialization in C language, C ++ is not treated as one of the following. c ++ has the following provisions:
1,The variable for initialization is defined. For example, int ivalue = 0; or extern int ivalue = 0; this is the definition of ivalue (with initialization ).
2,Variables without initialization and without the extern keyword are also defined. For example, int ivalue; even if the ivalue is defined, extern int ivalue can only be declared (with the extern keyword ).
Reason for non-conformity with C:
The reason why this standard does not conform to C is that C ++ has default constructor, such as class CT object. You cannot say it is the object declaration.
In addition, if C ++ is used for compilation, vs2010 meets the C ++ standard.
The Code is as follows:
//A.cpp fileintiValue;
//B.cpp fileintiValue;
//main.cpp file#include <stdio.h>extern intiValue;intmain(){printf("%d\n", iValue);}
This code will certainly fail to link, because according to the C ++ standard, the ivalue in A. CPP and B. cpp are both defined, so the re-definition error will naturally be reported during the link.
Question:
Why does vs not comply with the C language standards.