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 6 of 8.

Ashwini said:   1 decade ago
Please can you explain in detail how it will calculate the entire size of the union?

Sravan said:   1 decade ago
I can't able to get why 2+1 is added to 512. Can any one please explain briefly ?

Ishu said:   10 years ago
Why value of u?

I means the entire size of union? Can someone help me out?

Anonymous said:   8 years ago
If u.i displays the size of the Union then how to display the 'i' value?

Ritika said:   1 decade ago
Please explain in detail why is it giving 515 as the size of the union?

Rashmi said:   9 years ago
Why u.i would print size of the union? What exactly u.i means?

Komal said:   1 decade ago
ch is char type of variable then how can we assign it number?

Shaziya Hasan said:   5 years ago
How can we use %d for a character array? Can anyone explain?
(1)

Mesi said:   6 years ago
What will be the result if we add one more value u.ch[2]=4?

Gopi said:   1 decade ago
Anybody give brief explanation to calculate the union size?


Post your comments here:

Your comments will be displayed after verification.