Home > Backend Development > C++ > Why Do C 's `const` Objects Have Internal Linkage While C's Don't?

Why Do C 's `const` Objects Have Internal Linkage While C's Don't?

Mary-Kate Olsen
Release: 2024-12-01 01:48:14
Original
936 people have browsed it

Why Do C  's `const` Objects Have Internal Linkage While C's Don't?

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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template