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

Bhushan said:   1 decade ago
How we can calculate the value of u(I) at memory block 512?

Yamani said:   8 years ago
Thank you for your explanation. It helps me a lot @Nandini.

Akanksha said:   9 years ago
Why including only 512?

Kindly help by explaining this.

Rathika.b said:   1 decade ago
@nandhini:

I understood wat u told. but how 512 is got?

Jignesh said:   1 decade ago
How is it possible?
I is the variable and how 515 came?

Ayush said:   9 years ago
How can we get the size of the entire union by u.i?

Harika said:   6 years ago
Can anyone explain the memory allocation?

Please.

Soma said:   1 decade ago
Please explian how to find out the size of union.

Sumith said:   6 years ago
Why would the "i" get the entire size of union?
(2)

Kavya said:   9 years ago
How/why we get 512 and 515? Please explain me.


Post your comments here:

Your comments will be displayed after verification.