The following content is from deepseek. In the C standard, ‘const’ is primarily a compile-time constraint and commitment that informs the compiler of the programmer’s intention not to modify this object. It does not directly specify the storage location of the object or its immutability at runtime.
1. Core Description of the C Standard (C11 as an Example)
In the C11 standard (ISO/IEC 9899:2011),<span>const</span> is classified as atype qualifier, alongside <span>volatile</span> and <span>restrict</span>.
The <span>const</span> qualifier declares an object asnon-modifiable. If a program attempts to modify an object defined as a <span>const</span> type through a non-<span>const</span> lvalue, the behavior is undefined. This means that once an object is defined as <span>const</span> (for example, <span>const int a = 10;</span>), any direct or indirect modification of it (such as <span>a = 20;</span>) will lead toundefined behavior. A conforming compilermust diagnose direct modification attempts at compile time (with an error or warning).
2. Interpretation of ‘Undefined Behavior’ and Compiler Implementation
The standard defines ‘what is not allowed’, but leaves ‘what happens if it occurs’ to the implementation. This grants compilers significant freedom, leading to different implementation strategies.
Storage Location
-
The standard does not require that
<span>const</span>objects must be placed in read-only memory segments (such as .rodata). -
This is acompiler optimization and implementation strategy. Since the object will not be modified, placing it in read-only memory can:
-
Enhance security (prevent accidental or malicious modifications).
-
In embedded systems, it can sometimes save RAM (by placing it in ROM/Flash).
-
Allow sharing of memory pages between processes.
However, the compiler can place a <span>const</span> object in writable stack or heap memory as long as it ensures the program’sabstract machine behavior conforms to the standard (i.e., no modifications occur). For example, simple local <span>const</span> variables are typically on the stack.
Linkage
-
Provisions of the C Standard:
-
Object identifiers declared at file scope, if no storage class specifier
<span>static</span>is present, haveexternal linkage. -
However, there is anexception: if an object declaration includes the
<span>const</span>type qualifier and does not explicitly specify<span>extern</span>or<span>static</span>, then its linkage is defined by the implementation. -
Common Compiler Choice:
The vast majority of compilers (such as GCC, Clang, MSVC) choose to assigninternal linkage to such
<span>const</span>global variables. This is because if the default is external linkage, defining identically named const global variables in two different source files would lead to linkage errors. Assigning internal linkage makes const global variables more like ‘file-scope constants’, avoiding naming conflicts and enhancing safety. This aligns with the semantic of const ‘constant’. Therefore,<span>const int x = 5;</span>in compiler practice is equivalent to<span>static const int x = 5;</span>.
Conclusion
-
Commitment of the Standard: The C standard defines
<span>const</span>as acompile-time contract. It tells the compiler: ‘This object should not be modified’. Any code that violates this contract will triggerundefined behavior. -
Compiler Freedom and Strategy:
-
The compilermust reject all obvious modifications made through
<span>const</span>paths. -
The compilercan (but is not required to) utilize this information for optimizations, such as placing
<span>const</span>global variables in read-only storage. -
The compilercan (and generally does) assigninternal linkage to
<span>const</span>global variables without specified storage class, which is a wise and semantically consistent implementation-defined behavior.
Insights for Programmers:
-
View
<span>const</span>as astrict commitment, not a ‘suggestion’ that can be bypassed. Do not attempt to modify an object defined as<span>const</span>using type casting. -
Understand the fundamental philosophical difference between
<span>const</span>and<span>#define</span>: one is atyped variable, while the other isuntyped text replacement.
The compiler strictly adheres to the C standard regarding ‘non-modifiability’ and ‘undefined behavior’, and based on this, utilizes the freedom defined by the implementation (such as linkage and storage location) to generate optimal and safe code.