<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Snacks - static</title>
    <subtitle>Snack-size learning for a fast-paced world.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://snacks.devtestonly.uk/tags/static/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://snacks.devtestonly.uk"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-09-30T02:24:24+00:00</updated>
    <id>https://snacks.devtestonly.uk/tags/static/atom.xml</id>
    <entry xml:lang="en">
        <title>The `static` keyword in C23</title>
        <published>2026-09-30T02:24:24+00:00</published>
        <updated>2026-09-30T02:24:24+00:00</updated>
        
        <author>
          <name>someone</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://snacks.devtestonly.uk/c/static-keyword/"/>
        <id>https://snacks.devtestonly.uk/c/static-keyword/</id>
        
        <content type="html" xml:base="https://snacks.devtestonly.uk/c/static-keyword/">&lt;h2 id=&quot;the-static-keyword&quot;&gt;The &lt;code&gt;static&lt;/code&gt; keyword&lt;/h2&gt;
&lt;p&gt;In C, the static keyword modifies either the lifetime (how long a variable stays in memory) or the visibility/linkage (what parts of the program can see it) of a variable or function. [1, 2]
Its behavior changes completely based on where you place it in your code. [1]&lt;/p&gt;
&lt;h3 id=&quot;1-inside-a-function-static-local-variables&quot;&gt;1. Inside a Function (Static Local Variables)&lt;/h3&gt;
&lt;p&gt;When used inside a function, static changes the variable’s storage duration from automatic (stack-allocated) to static storage duration (stored in the data segment). [2, 3]&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What it does&lt;/strong&gt;: The variable is initialized only once. It retains its value across multiple function calls instead of being destroyed when the function exits.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scope&lt;/strong&gt;: It is still private to that function; other functions cannot access it directly. [1, 4, 5, 6, 7]&lt;/li&gt;
&lt;/ul&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; counter&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(#0000FF, #FF7B72);&quot;&gt;    static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; int&lt;/span&gt;&lt;span&gt; count &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 style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Initialized ONLY on the first call&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    count&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&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;%d&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; count&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&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;    counter&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; Prints 1&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;    counter&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; Prints 2&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;    counter&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; Prints 3&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;h3 id=&quot;2-outside-a-function-global-scope-internal-linkage&quot;&gt;2. Outside a Function / Global Scope (Internal Linkage)&lt;/h3&gt;
&lt;p&gt;By default, global variables and functions in C have extern (external) linkage, meaning they can be accessed from other source files (.c files) in the same project. [8, 9]&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What it does&lt;/strong&gt;: Placing static before a global variable or function limits its visibility strictly to the current translation unit (the specific .c file it is compiled in). [1, 9]&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why use it&lt;/strong&gt;: It acts like a private modifier in object-oriented languages. It hides internal implementation details and prevents naming collisions if another file happens to use the same variable or function name. [1, 8, 10]&lt;/li&gt;
&lt;/ul&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; file_helper.c&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;static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; int&lt;/span&gt;&lt;span&gt; internal_state &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 style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#008000, #8B949E);&quot;&gt; Other .c files cannot see or modify this&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;static&lt;/span&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; do_background_math&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&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; Private helper function; invisible to the linker outside this file&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;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; public_api_function&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(#795E26, #D2A8FF);&quot;&gt;    do_background_math&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; Allowed here&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;h3 id=&quot;3-inside-array-declarations-in-function-parameters-modern-c-c99&quot;&gt;3. Inside Array Declarations in Function Parameters (Modern C / C99+)&lt;/h3&gt;
&lt;p&gt;In modern C (C99 and newer), static has a lesser-known third use inside function parameter array brackets.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What it does&lt;/strong&gt;: It acts as a hint to the compiler optimizer, guaranteeing that the array argument passed into the function will always contain at least the specified number of elements and will not be NULL.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why use it&lt;/strong&gt;: It allows the compiler to generate highly optimized assembly code (like using SIMD vector instructions) because it doesn’t have to worry about null checks or out-of-bounds safety checks for that boundary.&lt;/li&gt;
&lt;/ul&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; The compiler assumes &amp;#39;arr&amp;#39; is never NULL and has AT LEAST 4 elements&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; process_coordinates&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(#001080, #FFA657);&quot;&gt; arr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 4&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(#AF00DB, #FF7B72);&quot;&gt;    for&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&gt; i &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; i &lt;/span&gt;&lt;span style=&quot;color: light-dark(#000000, #FF7B72);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 4&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i&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&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(#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(#001080, #FFA657);&quot;&gt; arr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&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;/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;h3 id=&quot;summary-table&quot;&gt;Summary Table&lt;/h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Context&lt;/th&gt;&lt;th&gt;What it Affects&lt;/th&gt;&lt;th&gt;Behavior&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Inside a function&lt;/td&gt;&lt;td&gt;Lifetime&lt;/td&gt;&lt;td&gt;Value persists across function calls; allocated once.&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Global variable / Function&lt;/td&gt;&lt;td&gt;Visibility (Linkage)&lt;/td&gt;&lt;td&gt;Restricts access to the current .c file only (Internal Linkage).&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Function argument array&lt;/td&gt;&lt;td&gt;Optimization / Safety&lt;/td&gt;&lt;td&gt;Guarantees the array is non-null and meets a minimum size requirement.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;[1] &lt;a rel=&quot;external&quot; href=&quot;https://www.youtube.com/watch?v=3E-r4GfvWOI&amp;amp;t=164&quot;&gt;https://www.youtube.com&lt;/a&gt;
[2] &lt;a rel=&quot;external&quot; href=&quot;https://www.youtube.com/watch?v=g4Dn2cwSrC4&quot;&gt;https://www.youtube.com&lt;/a&gt;
[3] &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Static_%28keyword%29&quot;&gt;https://en.wikipedia.org&lt;/a&gt;
[4] &lt;a rel=&quot;external&quot; href=&quot;https://www.reddit.com/r/explainlikeimfive/comments/si1ooi/eli5_what_exactly_is_a_static_variable_in/&quot;&gt;https://www.reddit.com&lt;/a&gt;
[5] &lt;a rel=&quot;external&quot; href=&quot;https://medium.com/@pratikkatkar13/mastering-static-keyword-in-c-from-basics-to-advanced-4bc9107eb80d&quot;&gt;https://medium.com&lt;/a&gt;
[6] &lt;a rel=&quot;external&quot; href=&quot;https://www.scaler.com/topics/cpp-static/&quot;&gt;https://www.scaler.com&lt;/a&gt;
[7] &lt;a rel=&quot;external&quot; href=&quot;https://www.w3schools.com/c/ref_keyword_static.php&quot;&gt;https://www.w3schools.com&lt;/a&gt;
[8] &lt;a rel=&quot;external&quot; href=&quot;https://www.beningo.com/131-using-the-static-keyword-in-c/&quot;&gt;https://www.beningo.com&lt;/a&gt;
[9] &lt;a rel=&quot;external&quot; href=&quot;https://www.youtube.com/watch?v=7Wx_o2p-6aY&amp;amp;t=56&quot;&gt;https://www.youtube.com&lt;/a&gt;
[10] &lt;a rel=&quot;external&quot; href=&quot;https://www.youtube.com/watch?v=CvVtkT_5J1s&amp;amp;t=252&quot;&gt;https://www.youtube.com&lt;/a&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;the-difference-between-passing-an-array-with-n-and-static-n&quot;&gt;The difference between passing an array with &lt;code&gt;[N]&lt;/code&gt; and &lt;code&gt;[static N]&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;When you pass an array to a function in C, the compiler ignores any size number you put inside standard brackets like &lt;code&gt;[N]&lt;/code&gt; and automatically treats it as a plain pointer (*).
The difference between &lt;code&gt;[static N]&lt;/code&gt; and &lt;code&gt;[N]&lt;/code&gt; comes down to what the compiler enforces and how it optimizes the code.&lt;/p&gt;
&lt;h3 id=&quot;comparison-breakdown&quot;&gt;Comparison Breakdown&lt;/h3&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Feature&lt;/th&gt;&lt;th&gt;Standard Array Syntax &lt;code&gt;[N]&lt;/code&gt;&lt;/th&gt;&lt;th&gt;Modern Array Syntax &lt;code&gt;[static N]&lt;/code&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;What the compiler sees&lt;/td&gt;&lt;td&gt;A standard pointer (int *arr)&lt;/td&gt;&lt;td&gt;A pointer that is never NULL and has at least N elements&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Allows NULL?&lt;/td&gt;&lt;td&gt;Yes (can cause a crash if you don’t check)&lt;/td&gt;&lt;td&gt;No (passing NULL is undefined behavior/compiler warning)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Enforces Size?&lt;/td&gt;&lt;td&gt;No (you can pass an array of size 1 or 2 to &lt;code&gt;[4]&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;Yes (compilers will warn you if the passed array is too small)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Compiler Optimization&lt;/td&gt;&lt;td&gt;Standard optimization&lt;/td&gt;&lt;td&gt;High optimization (enables aggressive loop unrolling and SIMD auto-vectorization)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;h3 id=&quot;detailed-differences&quot;&gt;Detailed Differences&lt;/h3&gt;
&lt;h4 id=&quot;1-compiler-warnings-code-safety&quot;&gt;1. Compiler Warnings &amp;amp; Code Safety&lt;/h4&gt;
&lt;p&gt;Standard syntax does not protect you from out-of-bounds errors or null pointers, whereas static acts as a static analysis contract.&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(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; process_normal&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(#001080, #FFA657);&quot;&gt; arr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;4&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; Compiled exactly as: void process_normal(int *arr);&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; process_static&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(#001080, #FFA657);&quot;&gt; arr&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 4&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&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(#0000FF, #FF7B72);&quot;&gt;    int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; small&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;2&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; {&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 style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt; 2&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 class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt;    process_normal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #79C0FF);&quot;&gt;NULL&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; Compiles perfectly fine (might crash at runtime)&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;    process_normal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;small&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; Compiles perfectly fine (might crash at runtime)&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(#795E26, #D2A8FF);&quot;&gt;    process_static&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #79C0FF);&quot;&gt;NULL&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; WARNING: Null pointer passed to a static argument&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;    process_static&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;small&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; WARNING: Array argument is smaller than prototype requires&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;h4 id=&quot;2-performance-and-optimization-the-main-reason-to-use-it&quot;&gt;2. Performance and Optimization (The Main Reason to Use It)&lt;/h4&gt;
&lt;p&gt;Because &lt;code&gt;int arr[static 4]&lt;/code&gt; guarantees that the pointer points to a valid, continuous block of memory with at least 4 integers, the compiler can make strong assumptions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Null Checks&lt;/strong&gt;: The compiler completely skips checking if &lt;code&gt;arr == NULL&lt;/code&gt; before looping.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vectorization&lt;/strong&gt;: The compiler knows it can safely read 4 elements at once using CPU vector registers (SIMD) without accidentally triggering a segmentation fault by reading past the end of the array.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;a-common-misconception&quot;&gt;A Common Misconception&lt;/h3&gt;
&lt;p&gt;Using &lt;code&gt;[static N]&lt;/code&gt; does not change the memory allocation of the array. It does not make the array live forever like a static local variable does. It is strictly a contract type constraint for function parameters.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;no-difference-between-n-and-in-function-parameters&quot;&gt;No Difference Between &lt;code&gt;[N]&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt; in Function Parameters&lt;/h2&gt;
&lt;p&gt;In C, passing &lt;code&gt;int A[N]&lt;/code&gt; as a function parameter is just syntactic sugar for passing &lt;code&gt;int* A&lt;/code&gt;. This behavior is known as array decay.
The compiler completely ignores the number N inside standard brackets. It throws that information away and rewrites the parameter as a plain pointer before it even compiles the code.&lt;/p&gt;
&lt;h3 id=&quot;proof-by-compiler-behavior&quot;&gt;Proof by Compiler Behavior&lt;/h3&gt;
&lt;p&gt;To the C compiler, the following three function declarations are 100% identical:&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(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; process_array&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;A&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; 1. Plain pointer syntax&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; process_array&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(#001080, #FFA657);&quot;&gt; A&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&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; 2. Open array syntax (decays to pointer)&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; process_array&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(#001080, #FFA657);&quot;&gt; A&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#098658, #79C0FF);&quot;&gt;100&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; 3. Fixed array syntax (STILL decays to pointer)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because they all evaluate to &lt;code&gt;int *A&lt;/code&gt;, you get zero guarantees:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;You can pass NULL without any compiler warnings.&lt;/li&gt;
&lt;li&gt;You can pass an array that is far too small (e.g., an array of only 2 elements to a function expecting &lt;code&gt;A[100]&lt;/code&gt;), and the compiler will remain completely silent.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;why-does-c-do-this&quot;&gt;Why does C do this?&lt;/h3&gt;
&lt;p&gt;This is a legacy design choice from the earliest days of C. Passing an entire array by value (copying every single element onto the stack) would be incredibly slow for large datasets. To keep the language fast, C was designed to automatically pass arrays by reference (a pointer to the first element).
Because standard brackets give you no protection, you traditionally have to pass the size explicitly:&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(#0000FF, #FF7B72);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#795E26, #D2A8FF);&quot;&gt; legacy_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(#000000, #FF7B72);&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt;A&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0000FF, #FF7B72);&quot;&gt; size_t&lt;/span&gt;&lt;span style=&quot;color: light-dark(#001080, #FFA657);&quot;&gt; size&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; The traditional, manual C way&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is exactly why &lt;code&gt;[static N]&lt;/code&gt; was introduced in modern C—it is the only way to tell the compiler, “Hey, I am actually passing an array of a specific size, please enforce it and optimize around it!”&lt;/p&gt;
</content>
        
    </entry>
</feed>
