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

Nandini said:   1 decade ago
Hi friends,

Here ch[0]=3 the binary value of 3 = 00000011=011 and ch[1]=2 the binary value of 2=010=00000010.

Now we can call the size of union i.e, 00000010 00000011.

See the fig so 1*1+2*1+512*1=515.

We are taking only 512, 1 and 2 because at these places only we have 1's remaing places we have zero. If we take the remaing also it'll multipled by 0 anyway we can get 0 for that values.

Anonymus said:   1 decade ago
Can you tell me why the initialisations for ch[0], i, j are not working?

#include<stdio.h>
int main()
{
union a
{
int i, j;
char 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;
}

O/P:

2, 5, 1282, 1282

Gaurav said:   9 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.

Gangadhararao said:   1 decade ago
What is the output of the program given below:

#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;
}

Explain in detail about the output.

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.

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.

Jenifer said:   10 years 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?

Devang r dixit said:   10 years ago
Please give output with explanation:

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

Theevan said:   1 decade ago
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);

I got these output...not the mentioned in the explanation.

3, 2, 1075053059.......why?

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


Post your comments here:

Your comments will be displayed after verification.