Site icon Learn Web Development & Programming, Artificial Intelligence, WordPress, Shopify Articles

What is a static variable?

A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.

<span class="kwd">void</span><span class="pln"> f</span><span class="pun">()</span> <span class="pun">{</span> 
   <span class="kwd">static</span> <span class="kwd">int</span><span class="pln"> i</span><span class="pun">;</span> 
   <span class="pun">++</span><span class="pln">i</span><span class="pun">;</span><span class="pln"> 
   printf</span><span class="pun">(“%</span><span class="pln">d </span><span class="pun">“,</span><span class="pln">i</span><span class="pun">);</span> 
<span class="pun">}</span>

If a global variable is static then its visibility is limited to the same source code.

Exit mobile version