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.

Prakash said:   8 years ago
Could anyone please explain me the output of this program?

#include<stdio.h>
int main()
{
union a
{
int i;
char ch;
};
union a u;
printf("%d,%f\n",sizeof(u),u.i);
return 0;
}

o/p: 4 23

Anonymous said:   8 years ago
Why u. i prints the size of the Union in the first place?

Can anyone answer it? please.

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

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

Preethi said:   8 years ago
Why we find the size of the union in coding doesn't mention about size only the variable value?

Sjohnson said:   8 years ago
@All.

If sizeof(int) > 4 then the upper bits of a.i is not initialized and the value of u.i can be greater than 0x0203. For example, the 64-bit compiler on my system produces the value 0x7c0203.

However, if I set u.i = 0 before setting u.ch[0], u.ch[1] then it works.

Aaryan said:   8 years ago
Hi friends, some guys have a problem that how we got a 512?

So the answer is like.
From right to left start from 1 and double the size after ++.
For eg...... 512 256 128 64 32 16 8 4 2 1

Binary is = 00000010 00000011.
512*1+0+0+0+0+0+0+0+2*1+1*1.
=512+2+1.
=515.

Ajay gupta said:   7 years ago
@All.

As explained that how it became 515 and I understood it. But why we need to calculate the size of the union while we are accessing the value of the variable I in the union u. why the size and how u, I is related to the size.

Mostafa Hamoda said:   7 years ago
Guys let's take it easy.

The function of the union is to calculate the size of each of the variable contained in it and find the biggest size of them and then create a place on memory of that size, so in this example a 4 bytes space of memory is reserved for our union, in our usage we created an object (u) which have access to our four bytes, our first assignment was to put a value of 3(u.ch[0]) in the first byte because our array is of type character so our object will deal with only the first byte and initialize the rest of them with (zeros), and the second byte with 2 (u.ch[1]) now in our memory we should have a shape like this:

00000000 00000000 00000010 00000011
(ch[1]) (ch[0])
[ ONE INTEGER].

Now, when we used our object to access the memory and deal with info side as if it were an integer.

So the value would be (1*2^0+1*2^2+1*2^9) = 515.
(3)

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


Post your comments here:

Your comments will be displayed after verification.