<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Richard Toth</title>
    <description>The latest articles on DEV Community by Richard Toth (@tothricsaj).</description>
    <link>https://dev.to/tothricsaj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F98036%2Fa72d6c3e-dff4-407e-915c-9aa098c20d8b.jpg</url>
      <title>DEV Community: Richard Toth</title>
      <link>https://dev.to/tothricsaj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tothricsaj"/>
    <language>en</language>
    <item>
      <title>Static in the house - C++ tales #1</title>
      <dc:creator>Richard Toth</dc:creator>
      <pubDate>Wed, 17 Jun 2026 08:48:15 +0000</pubDate>
      <link>https://dev.to/tothricsaj/static-in-the-house-c-tales-1-3dgh</link>
      <guid>https://dev.to/tothricsaj/static-in-the-house-c-tales-1-3dgh</guid>
      <description>&lt;h3&gt;
  
  
  What is static
&lt;/h3&gt;

&lt;p&gt;In C++, &lt;code&gt;static&lt;/code&gt; is a keyword that primarily affects a variable's storage duration. A variable declared as &lt;code&gt;static&lt;/code&gt; exists for the entire lifetime of the program.&lt;/p&gt;

&lt;p&gt;However, there is a little more to it. Depending on where it is used, &lt;code&gt;static&lt;/code&gt; can also affect a symbol's visibility, which is known as &lt;em&gt;linkage&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static: The Local Hero
&lt;/h3&gt;

&lt;p&gt;When a variable is declared inside a function, it can only be accessed within that function. This is true for both automatic and static local variables. The difference lies in their lifetime. Automatic variables are created every time the function is called and destroyed when the function returns. A local static variable, on the other hand, is initialized only once and remains alive until the program terminates.&lt;/p&gt;

&lt;p&gt;Although the variable continues to exist after the function returns, it is still only accessible from within the function where it was declared. Because its lifetime extends beyond the function call, a local static variable is not typically stored on the stack. It also preserves its value between function calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example time
&lt;/h3&gt;

&lt;p&gt;Here is a little showcase code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstdio&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test_static&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;other_count&lt;/span&gt;&lt;span class="p"&gt;{};&lt;/span&gt;

  &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;other_count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The test_static func is called %d times!!!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is other_count &amp;gt;&amp;gt; %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other_count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;test_static&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;test_static&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;test_static&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...and the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;The test_static func is called 1 &lt;span class="nb"&gt;times&lt;/span&gt;&lt;span class="o"&gt;!!!&lt;/span&gt;
This is other_count &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; 1
&lt;span class="nt"&gt;-------------------------------------------------------&lt;/span&gt;
The test_static func is called 2 &lt;span class="nb"&gt;times&lt;/span&gt;&lt;span class="o"&gt;!!!&lt;/span&gt;
This is other_count &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; 1
&lt;span class="nt"&gt;-------------------------------------------------------&lt;/span&gt;
The test_static func is called 3 &lt;span class="nb"&gt;times&lt;/span&gt;&lt;span class="o"&gt;!!!&lt;/span&gt;
This is other_count &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; 1
&lt;span class="nt"&gt;-------------------------------------------------------&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have a &lt;em&gt;test_static&lt;/em&gt; function with a static and automatic variable: &lt;em&gt;count&lt;/em&gt; and &lt;em&gt;other_count&lt;/em&gt;. In the output, it can be easily what happens with this two little guy. The count - what is a static variable of the test_static funtion - keeps the value and in every function call  it is incremented and keeps and increase the value while the other_count is incremented, too although it cannot keep the own value between the function calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static, the member
&lt;/h3&gt;

&lt;p&gt;The behavior of the static is pretty similar in classes but have some differencies, too.&lt;/p&gt;

&lt;p&gt;Let's check this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlanetOfTheSolarSystem&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;public:&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;our_star&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;planet_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;PlanetOfTheSolarSystem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;planet_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;get_planet_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;planet_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;PlanetOfTheSolarSystem&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;our_star&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Sun (other name is Sol)"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;PlanetOfTheSolarSystem&lt;/em&gt; class has two variable &lt;em&gt;our_star&lt;/em&gt; and the &lt;em&gt;planet_name&lt;/em&gt;. While the planet_name is declared in the constructor, the static varialbe - our_star - is declared outside of the class, and a different syntax is used to it. The reason is the static member belongs to class and not to the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_planets_star&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;planet_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;star_of_the_planet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Star of the %s is %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;planet_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c_str&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;star_of_the_planet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c_str&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;PlanetOfTheSolarSystem&lt;/span&gt; &lt;span class="n"&gt;venus&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Venus"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="n"&gt;PlanetOfTheSolarSystem&lt;/span&gt; &lt;span class="n"&gt;earth&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Earth"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="n"&gt;print_planets_star&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;venus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_planet_name&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;venus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;our_star&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;print_planets_star&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;earth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_planet_name&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;earth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;our_star&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Star of the Venus is Sun &lt;span class="o"&gt;(&lt;/span&gt;other name is Sol&lt;span class="o"&gt;)&lt;/span&gt;
Star of the Earth is Sun &lt;span class="o"&gt;(&lt;/span&gt;other name is Sol&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example code and the output show us the essence of the static member. The venus and the earth objects point to same variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In a nutshell, &lt;code&gt;static&lt;/code&gt; is an essential C++ feature for creating data that must persist throughout the program's execution. When used inside functions, it allows values to survive between function calls. When used as a class member, it creates data shared by all instances of the class. In other contexts, it can also affect symbol visibility and linkage.&lt;/p&gt;

&lt;p&gt;Understanding &lt;code&gt;static&lt;/code&gt; is important because it appears frequently in real-world C++ code and forms the basis of several common programming techniques and design patterns.&lt;/p&gt;

&lt;p&gt;Thanks for reading, and keep learning!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programing</category>
    </item>
  </channel>
</rss>
