title: union initialization in c
body: <p>i was wondering whether union variable will be initialized like a structure variable or not...</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  int main(void)
  {
   struct a
   {
   int i;
   char c;
   };
  struct a ob={4};
  printf("%d",ob.c);
  </code></pre>
  
  <p>}<br>
  the above code gives 0 as output..<br>
  so when i is intialized c also gets intialized..<br>
  in the below code...if the union member integer also gets intialized with the character array this snippet would give the output 515...<br>
  (i verified it by allocating memory for the union variable using malloc..it works fine.)  </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  int main(void)
  {
  union a
  {
  int i;
  char c[2];
   };
   union a ob;
   ob.ch[0]=3;
   ob.ch[1]=2;
   printf("%d",ob.i);
   return 0;
  }
  </code></pre>
  
  <p>but without allocating memory whether it could intialize the int i or not..(the hex value of int i in this code is set to 0x990203).<br>
  i think that 99 is the result that shows that the higher bits are not intialized..<br>
  am i correct?.. </p>