title: Why are local variables initialized to 0 in g++ compiler for c++ in ubuntu linux?
body: <p>As per any book on c++, any local variable of c++ which is not initialized will contain a garbage value. However, check out this following program:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  float a;
  class A
  {
  public:
  float b;
  };
  int main()
  {
  float c;
  static float d;
  static float e = 0;
  A f;
  cout&lt;&lt;"\n global a : "&lt;&lt;a&lt;&lt;"\n class variable b : "&lt;&lt;f.b;
  cout&lt;&lt;"\n local c : "&lt;&lt;c&lt;&lt;"\n static local d : "&lt;&lt;d
  &lt;&lt;"\n static initialized local e : "&lt;&lt;e;
  }
  </code></pre>
  
  <p>If compiled with g++ on ubuntu linux it gives the following output:</p>
  
  <pre><code>global a : 0
  class variable b : 6.94896e-36
  local c : 0
  static local d : 0
  static initialized local e : 0
  </code></pre>
  
  <p>The strange thing is that it gives a 0 value for the local variable c, whereas this should be uninitialized and contain some garbage value. The same program works differently with visual c++ for windows and gives a garbage value for c which is what you would expect.</p>