Skip to content
Related Articles

Related Articles

C Quiz – 112 | Question 5
  • Difficulty Level : Medium
  • Last Updated : 02 Nov, 2015

Pick the best statement for the below program:




#include "stdio.h"
  
int main()
{
 union {int i1; int i2;} myVar = {.i2 =100};
 printf("%d %d",myVar.i1, myVar.i2);
 return 0;
}

(A) Compile error due to incorrect syntax of initialization.
(B) No compile error and it’ll print “0 100”.
(C) No compile error and it’ll print “100 100”.


Answer: (C)

Explanation: Since fields/members of union share same memory, both i1 and i2 refer to same location. Also, since both i1 and i2 are of same type, initializing one would initialize the other as well implicitly. So answer is C.

Quiz of this Question

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :