Open In App
Related Articles

C Quiz – 112 | Question 5

Improve Article
Save Article
Like Article

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;


Compile error due to incorrect syntax of initialization.


No compile error and it’ll print “0 100”.


No compile error and it’ll print “100 100”.

Answer: (C)


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
Please comment below if you find anything wrong in the above post

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads