<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1356837403028931008</id><updated>2024-02-07T20:26:39.671-08:00</updated><title type='text'>Things I should Have Known Before</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tishkb.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1356837403028931008/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://tishkb.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Mayank</name><uri>http://www.blogger.com/profile/03317633641453574076</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1356837403028931008.post-969024525354864714</id><published>2011-10-30T03:51:00.000-07:00</published><updated>2011-10-30T04:04:13.598-07:00</updated><title type='text'>Static Variables in C/C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Static Variables in C/C++&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgS7NoqUpzTOqBHqekQeaJUdy67ivHn6O_CtT7N5f4yG17vlkhgVJf7uyM_TTTXVeZbzc40uhA89A93_IUGT7TnU6knSXCyJoebjL02VAKYjfOy_5i1SkHp9118lfSbzqzy2XpDc2dSwwgu/s1600/static.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; float: right; margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;200&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgS7NoqUpzTOqBHqekQeaJUdy67ivHn6O_CtT7N5f4yG17vlkhgVJf7uyM_TTTXVeZbzc40uhA89A93_IUGT7TnU6knSXCyJoebjL02VAKYjfOy_5i1SkHp9118lfSbzqzy2XpDc2dSwwgu/s200/static.png&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;exist.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;are pretty neat.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;are not equivalent to global variables, they can have restricted scope.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;have some issues.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Their e&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;xistence&lt;/span&gt;&lt;br /&gt;
I was not aware of them. It was only through a Microsoft hiring test that I realized that such a thing might exist - if there are three questions, where &lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;static&lt;/span&gt;&lt;/b&gt; keyword is used then you are bound to wonder if it does something special. I took the chance and it does exactly what I assumed.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Their neatness&lt;/span&gt;&lt;br /&gt;
Okay, so from &lt;a href=&quot;http://en.wikipedia.org/wiki/Static_variable&quot;&gt;wikipedia&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #f9f9f9; font-family: monospace, &#39;Courier New&#39;; font-size: 12px;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;de1&quot; style=&quot;background-attachment: initial; background-clip: initial; background-color: initial; background-image: none; background-origin: initial; border-bottom-color: white; border-bottom-style: none; border-bottom-width: 0px; border-left-color: white; border-left-style: none; border-left-width: 0px; border-right-color: white; border-right-style: none; border-right-width: 0px; border-top-color: white; border-top-style: none; border-top-width: 0px; color: black; font-family: monospace, &#39;Courier New&#39; !important; font: normal normal normal 1em/1.2em monospace; line-height: normal; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: top;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #f9f9f9; font-family: monospace, &#39;Courier New&#39;; font-size: 12px;&quot;&gt;&lt;span class=&quot;co2&quot; style=&quot;color: #339933;&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
 
&lt;span class=&quot;kw4&quot; style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; func&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kw4&quot; style=&quot;color: #993333;&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kw4&quot; style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; x &lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot; style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;co1&quot; style=&quot;color: #666666; font-style: italic;&quot;&gt;// x is initialized only once across 3 calls of func()&lt;/span&gt;
        &lt;span class=&quot;kw3&quot; style=&quot;color: #000066;&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;st0&quot; style=&quot;color: red;&quot;&gt;&quot;%d&lt;span class=&quot;es1&quot; style=&quot;color: #000099; font-weight: bold;&quot;&gt;\n&lt;/span&gt;&quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; x&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;co1&quot; style=&quot;color: #666666; font-style: italic;&quot;&gt;// outputs the value of x&lt;/span&gt;
        x &lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;=&lt;/span&gt; x &lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nu0&quot; style=&quot;color: #0000dd;&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;kw4&quot; style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; main&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kw4&quot; style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt; argc&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kw4&quot; style=&quot;color: #993333;&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;kw4&quot; style=&quot;color: #993333;&quot;&gt;const&lt;/span&gt; argv&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;{&lt;/span&gt;
        func&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;co1&quot; style=&quot;color: #666666; font-style: italic;&quot;&gt;// prints 0&lt;/span&gt;
        func&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;co1&quot; style=&quot;color: #666666; font-style: italic;&quot;&gt;// prints 1&lt;/span&gt;
        func&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;co1&quot; style=&quot;color: #666666; font-style: italic;&quot;&gt;// prints 2&lt;/span&gt;
        &lt;span class=&quot;kw1&quot; style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nu0&quot; style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy0&quot; style=&quot;color: #339933;&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot; style=&quot;color: #009900;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #f9f9f9; font-family: monospace, &#39;Courier New&#39;; font-size: 12px;&quot;&gt;
&lt;/span&gt;&lt;/blockquote&gt;
&amp;nbsp;Things to notice:&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;x&lt;/span&gt;&lt;/b&gt; is initialized once.&lt;/li&gt;
&lt;li&gt;The declaration statement is ignored(!!) every other time.&lt;/li&gt;
&lt;li&gt;Can be modified.&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Their&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;specialty&lt;/span&gt;&lt;br /&gt;
Again from wikipedia, there are three scope levels for &lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;static&lt;/span&gt;&lt;/b&gt; variables, something that &lt;i&gt;vanilla&amp;nbsp;&lt;/i&gt;global variables do not provide:&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;Static&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;b&gt;Global&lt;/b&gt;&lt;/span&gt;&amp;nbsp;Variables&lt;/b&gt;: Have internal linkage (File level scope). Quite similar to global variables you say?&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Static Local Variables&lt;/b&gt;: Have function scope, just like vanilla variables(Called &lt;a href=&quot;http://en.wikipedia.org/wiki/Automatic_variable&quot;&gt;Automatic Variables&lt;/a&gt;&amp;nbsp;- More fodder for&amp;nbsp;enlightenment?) inside the function, but the last value of the variables in the function will be &lt;i&gt;carried&lt;/i&gt;&amp;nbsp;till the next time the function is called (Just like the example).&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Static Member Variables&lt;/b&gt;: are class variables marked static. They are shared between all class instances, as opposed to Instance Variables that are shared only across the instance.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Their quirks&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
The very next question that came to my mind: if they seem so awesome, why are they not that popular/talked about (in CS101 and elsewhere). Because of some of their quirks[from SO]:&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Order of initialization not controllable, if there are more than one of these and if they are interdependent - then you are in trouble.&lt;/li&gt;
&lt;li&gt;Their handling becomes tougher in a multi-threaded environment.&lt;/li&gt;
&lt;/ul&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;So?&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;I will be using them with care, for use cases similar to the one reported &lt;a href=&quot;http://stackoverflow.com/questions/389779/should-i-use-static-data-members-c&quot;&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Of course, using static variables does reduce the readability of the code slightly - or may be it is just me. If it is true for others as well, it makes sense that they are not that popular.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;I should have seen:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Static_variable&quot;&gt;Wikipedia: Static Variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://stackoverflow.com/questions/389779/should-i-use-static-data-members-c&quot;&gt;StackOverflow: Should I use static data members? (C++)&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Weirdness!&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
So, I did an experiment:&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;pre style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; f&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; c&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
  &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; oldC &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; c&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; s &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;c&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
  s &lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; c&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;s:&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;s&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;c:&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;oldC:&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;oldC&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;endl&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
  f&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
  f&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
  f&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/blockquote&gt;
It gives output:&lt;br /&gt;
&lt;div style=&quot;overflow-x: auto; overflow-y: auto;&quot;&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;./a.out&lt;br /&gt;s:4     c:2     oldC:1&lt;br /&gt;s:6     c:2     oldC:2&lt;br /&gt;s:9     c:3     oldC:3&lt;/span&gt;&lt;/blockquote&gt;
&lt;pre style=&quot;background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; color: black;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;white-space: normal;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Times New Roman&#39;;&quot;&gt;&lt;b&gt;Weird thing: &lt;/b&gt;&lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;i&gt;oldC == c&lt;/i&gt;&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Times New Roman&#39;;&quot;&gt; for second and third function call, the statement with static declaration is completely ignored! I had thought that only the setting of value of s will be ignored, well I couldn&#39;t have guessed.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tishkb.blogspot.com/feeds/969024525354864714/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tishkb.blogspot.com/2011/10/static-variables-in-cc.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1356837403028931008/posts/default/969024525354864714'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1356837403028931008/posts/default/969024525354864714'/><link rel='alternate' type='text/html' href='http://tishkb.blogspot.com/2011/10/static-variables-in-cc.html' title='Static Variables in C/C++'/><author><name>Mayank</name><uri>http://www.blogger.com/profile/03317633641453574076</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgS7NoqUpzTOqBHqekQeaJUdy67ivHn6O_CtT7N5f4yG17vlkhgVJf7uyM_TTTXVeZbzc40uhA89A93_IUGT7TnU6knSXCyJoebjL02VAKYjfOy_5i1SkHp9118lfSbzqzy2XpDc2dSwwgu/s72-c/static.png" height="72" width="72"/><thr:total>0</thr:total></entry></feed>