<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Snacks - const</title>
    <subtitle>Snack-size learning for a fast-paced world.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://snacks.devtestonly.uk/tags/const/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://snacks.devtestonly.uk"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-10-01T03:43:31+00:00</updated>
    <id>https://snacks.devtestonly.uk/tags/const/atom.xml</id>
    <entry xml:lang="en">
        <title>The `const` in modern C</title>
        <published>2026-10-01T03:43:31+00:00</published>
        <updated>2026-10-01T03:43:31+00:00</updated>
        
        <author>
          <name>someone</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://snacks.devtestonly.uk/c/const-in-modern-c/"/>
        <id>https://snacks.devtestonly.uk/c/const-in-modern-c/</id>
        
        <content type="html" xml:base="https://snacks.devtestonly.uk/c/const-in-modern-c/">&lt;p&gt;An object of a &lt;code&gt;const&lt;/code&gt;-qualified type in C is read-only, meaning the compiler prevents you from modifying it through that specific identifier. However, this qualification acts as a contractual interface for your code rather than an absolute physical lock on the underlying memory.&lt;/p&gt;
&lt;p&gt;To master modern C, particularly with the standard updates in C23, engineering students must distinguish between variables that are protected from modification at runtime and true compile-time constants.&lt;/p&gt;
&lt;h3 id=&quot;1-const-as-a-programming-contract&quot;&gt;1. &lt;code&gt;const&lt;/code&gt; as a Programming Contract&lt;/h3&gt;
&lt;p&gt;The primary engineering use of &lt;code&gt;const&lt;/code&gt; is to enforce data safety across function boundaries. When you pass a pointer to a function, &lt;code&gt;const&lt;/code&gt; guarantees that the function will not mutate the caller’s original data.&lt;/p&gt;
&lt;p&gt;Modern C formatting often relies on writing &lt;code&gt;const&lt;/code&gt; immediately after the type it qualifies, which clarifies exactly what is being protected. For example, &lt;code&gt;char const*const&lt;/code&gt; specifies both that the characters cannot be altered and that the pointer itself cannot be redirected to a new memory address.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; The parameter &amp;#39;str&amp;#39; is a read-only pointer to read-only characters.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; print_message&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; str[0] = &amp;#39;H&amp;#39;; // ERROR: Cannot mutate the characters.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; str = &amp;quot;New&amp;quot;;  // ERROR: Cannot reassign the pointer itself.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; str&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;const&lt;/span&gt;&lt;span&gt; greeting &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Hello, Engineers&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    print_message&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;greeting&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this scenario, &lt;code&gt;const&lt;/code&gt; shifts potential runtime memory corruption bugs into immediate compile-time errors.&lt;/p&gt;
&lt;h3 id=&quot;2-c23-and-compile-time-guarantees-constexpr&quot;&gt;2. C23 and Compile-Time Guarantees: &lt;code&gt;constexpr&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;For decades, developers used &lt;code&gt;const&lt;/code&gt; or preprocessor &lt;code&gt;#define&lt;/code&gt; macros to declare constants, but both have flaws. Macros lack type safety, and a &lt;code&gt;const&lt;/code&gt;-qualified object simply means its value cannot be modified after initialization—it does not mean its value is known during compilation.&lt;/p&gt;
&lt;p&gt;C23 introduces the &lt;code&gt;constexpr&lt;/code&gt; construct, which results in read-only objects that are guaranteed to never change, because their values are fully determined at compile time. An integer constant expression must only evaluate objects that are declared with &lt;code&gt;constexpr&lt;/code&gt;, making it the required choice for array sizes or &lt;code&gt;switch&lt;/code&gt; case labels.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; constexpr guarantees the value is embedded at compile-time.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;constexpr &lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;double&lt;/span&gt;&lt;span&gt; PI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;14159265358979323846&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Valid: constexpr objects can be used to size arrays safely.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; circle_areas&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;constexpr &lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 10&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt; ]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C23 enforces strict type safety for &lt;code&gt;constexpr&lt;/code&gt; initialization. The initializer of a &lt;code&gt;constexpr&lt;/code&gt; object must fit the target type exactly without precision loss.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; ERROR: Compilation fails because significant digits after&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; the decimal point are lost when converting to an unsigned integer.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;constexpr &lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; PI_flat &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;14159265358979323846&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;3-the-reality-of-memory-aliasing&quot;&gt;3. The Reality of Memory Aliasing&lt;/h3&gt;
&lt;p&gt;A common misconception is that a &lt;code&gt;const&lt;/code&gt; qualification makes the physical memory completely immutable. In reality, the fact that a specific object is &lt;code&gt;const&lt;/code&gt;-qualified does not mean the compiler or run-time system might not change its value. Other parts of the program may see that exact same object without the qualification and change it.&lt;/p&gt;
&lt;p&gt;This phenomenon is known as aliasing. If two pointers point to the exact same memory address, but only one is &lt;code&gt;const&lt;/code&gt;-qualified, the data can mutate “behind the back” of the read-only pointer.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; execute_process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; status&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; mutable_alias&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Initial status: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; *&lt;/span&gt;&lt;span&gt;status&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Output: 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; The data is modified through a different pathway.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;    *&lt;/span&gt;&lt;span&gt;mutable_alias &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; The const pointer now reads a different value!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    printf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;Mutated status: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FF7B72);&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: light-dark(#EE0000, #FF7B72);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; *&lt;/span&gt;&lt;span&gt;status&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Output: 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; hardware_flag &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Passing the exact same memory address to both parameters.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    execute_process&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;hardware_flag&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;hardware_flag&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because of aliasing, &lt;code&gt;const&lt;/code&gt; should be viewed strictly as an access right applied to a specific variable name, not a physical hardware lock.&lt;/p&gt;
&lt;h3 id=&quot;implementation-strategy-for-modern-c&quot;&gt;Implementation Strategy for Modern C&lt;/h3&gt;
&lt;p&gt;To write safe and highly optimizable C code, adopt the following rules:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;constexpr&lt;/code&gt; for any fixed limit, mathematical constant, or configuration value known before compiling.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;const&lt;/code&gt; for data fetched at runtime (like sensor readings or user inputs) that should not be altered after its initial assignment.&lt;/li&gt;
&lt;li&gt;Systematically apply &lt;code&gt;const&lt;/code&gt; to pointer parameters in your function interfaces to prevent accidental memory mutation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;a-note-on-physical-memory-protection&quot;&gt;A Note on Physical Memory Protection&lt;/h3&gt;
&lt;p&gt;If the variable was originally declared as a global &lt;code&gt;const&lt;/code&gt;, the compiler might place it in a physical Read-Only section of memory (like &lt;code&gt;.rodata&lt;/code&gt;). If you try the cast-trick on that, the program will compile but crash at runtime with a Segmentation Fault. This physical memory protection and associated risk of crashing applies to several other unmutable entities in C:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;String Literals:&lt;/strong&gt; String literals are inherently read-only. However, for backward compatibility, their type does not explicitly include the &lt;code&gt;const&lt;/code&gt; keyword, making them an easy target for accidental mutation attempts that will result in a crash.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Literal Overlapping (Shared Memory):&lt;/strong&gt; Read-only object literals, including string literals and &lt;code&gt;const&lt;/code&gt;-qualified compound literals, may overlap in physical memory. The compiler is permitted to map identical literals (or even overlapping suffixes, such as “end” and “friend”) to the exact same memory address to save space. Forcing a mutation on one via a pointer cast could corrupt other seemingly unrelated variables throughout your program.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Temporary Objects:&lt;/strong&gt; Objects with a temporary lifetime, such as those returned by a function call to access a specific struct member, are strictly read-only.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#000000, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#A31515, #A5D6FF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt; Point&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Point &lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;get_origin&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Point p &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#AF00DB, #FF7B72);&quot;&gt;    return&lt;/span&gt;&lt;span&gt; p&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Returning a temporary object&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; In this example, get_origin() returns a temporary Point object (a rvalue)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Reading the value of a temporary object is valid:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;    int&lt;/span&gt;&lt;span&gt; m &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; get_origin&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;x&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Writing to a temporary object is INVALID&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; The compiler will report an error:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    get_origin&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 10&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;    //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; But, if you store the temporary object into a variable, you can modify it:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Point my_point &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; get_origin&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Store the temporary object into a variable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;    my_point&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #E6EDF3);&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 10&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt;               //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Valid, because &amp;#39;my_point&amp;#39; is an lvalue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;General Access Violations:&lt;/strong&gt; Modifying any unmutable object—whether it is a &lt;code&gt;const&lt;/code&gt;-qualified object, a string literal, or a temporary object—is classified as an access violation and will lead to program failure.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
</feed>
