title: What is Happening in this C++ Code? Why s.u.a = 20?
body: <p>I was expecting the output to look like: <code>10 20 10 20</code></p>
  
  <p>But the output came like this: <code>10 20 20 20</code></p>
  
  <p>What is happening behind the code?</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  typedef struct sdata{
      int a;
      int b;
      union udata{
          int a;
          int b;
      }u;
  }Data;
  
  int main()
  {
      Data s;
      s.a = 10;
      s.b = 20;
      s.u.a = 10;
      s.u.b = 20;
      cout &lt;&lt; s.a &lt;&lt; " " &lt;&lt; s.b &lt;&lt; " " &lt;&lt; s.u.a &lt;&lt; " " &lt;&lt; s.u.b;
      return 0;
  }
  </code></pre>