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:
78 comments Page 2 of 8.

Punya said:   9 years ago
Please check by running the same program with c compiler, the Garbage value will be printed for i.

Rajat said:   9 years ago
When we are changing char array to short array it's not doing the same thing. In my compiler size of int is 4 and short is 2.

Akanksha said:   10 years ago
Why including only 512?

Kindly help by explaining this.

Gaurav said:   10 years ago
I think this might not be entirely correct.

Different computers use different orderings for bytes (notice I am not saying bits).
A little endian machine will place the least significant byte first (so ch[0] will come before ch[1]), and a big endian machine will do the reverse.

So the answer cannot be determined without this information.

Jenifer said:   1 decade ago
How does the statement printf("%d %d %d", u.ch[0],u.ch[1],u.i); prints the size of union?

Any variable hold some garbage value without initialization.

Here we din't used sizeof operator too then how its calculating the size and printing. Any one please explain?

Naziya said:   9 years ago
Explanation to the given question is as follows. As we know union size is the biggest size of its element, hence integer is considered to be the biggest.

Here integer is considered to be 4 bytes which are equal to 32 bits (GCC compiler). The memory allocation is as follows. 0000000000000000000000010000000011.

Hence from the above figure u.ch[0] = 3 & u.ch[1]=2.

As the character is of 1 byte.

So in that 4 bytes itself u.ch[0] and u.ch[1] can be occupied, as we are considering the biggest element to b integer, in other words, memory is shared.

So when we see u.i value is equivalent to 512 (the value at 10th bit) +2 (the value at 1st bit) +1 (the value at 0th bit). Therefore 512 + 2 + 1 = 515.

Ishu said:   1 decade ago
Why value of u?

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

Ace said:   1 decade ago
#include<stdio.h>
int main()
{
union a
{
long int i, j;
int ch[2];
};

union a u;
u.ch[0] = 3;
u.i=1;u.j = 2;
u.ch[1] = 5 ;
printf( "%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.j, u.i );
return 0;
}

Ravali said:   1 decade ago
I am not able to understand. And @Nandini you have a good explanation but I have understood it partially can you explain t more deeply?

Kuldip said:   1 decade ago
I read all question and answer but still I can't get answer of why union share common memory to all data elements.


Post your comments here:

Your comments will be displayed after verification.