C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 1)
1.
What will be 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
515, 515, 4
Answer: Option
Explanation:

The system will allocate 2 bytes for the union.

The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below.

Discussion:
40 comments Page 2 of 4.

Bnr said:   9 years ago
Hear we are not assign any value to.

Int i; how to compiler assign 515 value.

Arpeeta Halder said:   10 years ago
In the above question:

union a
{
int i;
char ch[2];
};

Here, memory is allocated for int i, i.e 2 bytes now.

u.ch[0]=3;
u.ch[1]=2;

They are stored in memory allocated for int i (as char are of 1 bytes) they are placed in each bytes of integer i, now when we print.

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;

Then, first u.ch[0] is printed then u.ch[1] then u.i where u.ch[0] then u.ch[1] from right to left is stored in memory allocated for u.i is saved.

Arjun said:   1 decade ago
I understood how 515 is printed. I don't understand Why does doing u, i print that value?

Sabi said:   1 decade ago
I can't understand that. How 512 is came?

Dash said:   1 decade ago
main()
{
union a{
int i;
char ch[2];
union a u;
u.ch[0]=3;
u.ch[1]=2;
here as we know the max size will be allocated is 2 bytes as size of character array is 2. Each value first represented in binary form and then stored in alloted memory space.This memory space again subdivided into two depending on no.of characters, as here two so(each having 8bits for storage).

So now according to binary display it is always from left to
right so the storing for characters will start from left which will be followed towards right consecutively.

Illustration:

2^15..........2^9 2^8 2^7 2^6...........2^1 2^0
| | | |
1 0 1 1
so u.ch[0]=3.
u.ch[1]=2.
u.i=2^9+2^1+2^0(summation of memory locations containing 1).
=512.

Thank you.

Kumar said:   1 decade ago
u.ch[0]=3;
u.ch[1]=2; for calculating (u.i) why we are taking 2 as first and 3 as second value?

Tharindu said:   1 decade ago
I want to know how to write this code.

1st reduce the value of A by 1 then find sum A and B then incriment B by 1?

Rushabh said:   1 decade ago
But Why ch[0] is right side of ch[1]. Means why it is Right to left?

Akash said:   1 decade ago
@Ramkumar.

It depends if you print using %d then it will print the ASCII value of a and b or if you print using %c it will print actual character i.e a and b.

And if you print u.i it will print the 25185 (calculate as same process as above discuss).

Ramkumar said:   1 decade ago
u.ch[0]='a';

u.ch[1]='b';

Can anyone explain output for this definition instead of u.ch[0]=3;

u.ch[1]=2;?


Post your comments here:

Your comments will be displayed after verification.