C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 10)
10.
What is the output of the program?
#include<stdio.h>
int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u;
    u.ch[0] = 3;
    u.ch[1] = 2;
    printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
    return 0;
}
3, 2, 515
515, 2, 3
3, 2, 5
None of these
Answer: Option
Explanation:

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); It prints the value of u.ch[0] = 3, u.ch[1] = 2 and it prints the value of u.i means the value of entire union size.

So the output is 3, 2, 515.

Discussion:
77 comments Page 8 of 8.

Sameer said:   1 decade ago
@nandini.

Thanks a lot. Good explanation.

Radhika said:   1 decade ago
But why do u.i gives the size of union?

Alisha said:   1 decade ago
Please explain how we get value of i?

Jayakumar v said:   4 years ago
Good explanation. Thanks, everyone.

Moorthy said:   1 decade ago
Please explain this program fully.

Saila said:   6 years ago
Thank you @Mostafa Hamoda.

Hemant7298 said:   1 decade ago
Thanks Soma.


Post your comments here:

Your comments will be displayed after verification.