Internal Linkage Implied by Const in C Unlike in C
In C , const objects declared within a namespace have internal linkage, a departure from C's approach. This behavior stems from const objects' potential use as compile-time constants in C .
Appendix C of the C 11 standard (C.1.2) explains the rationale behind this change. The rule encourages programmers to explicitly initialize const objects, promoting consistency and allowing for their inclusion in header files shared across compilation units.
For instance, consider the following example:
const int var_a = 1; int var_b = 1;
Compiling this code with g -c test.cpp exports only the variable var_b. This is because const variables are not made available externally, unlike in C.
The above is the detailed content of Why Do C 's `const` Objects Have Internal Linkage While C's Don't?. For more information, please follow other related articles on the PHP Chinese website!