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;
}
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 6 of 8.
Deepak said:
1 decade ago
@anonymous..
In that program first we assign u.ch[0]=3 which is array which index 0. so it takes memory in first byte and which is 00000011.
And then u.i=1;
Which REPLACE previous assigned memory of ch[0] and now it become 00000001
And then u.j=2;
Again REPLACE previous assigned memory of i and now it become 00000010
And now u.ch[1]=5 it takes memory in second byte and it is 00000101.
NOW IN MEMORY (00000101)(00000010)
second first byte
AND NOW printf("%d,%d,%d,%d",u.ch[0],u.ch[1],u.i,u.j); statement run.
IN THIS STATEMENT u.ch[0] is print the value which is in first byte. so we convert first byte binary digit in decimal numbers and get 2. so "u.ch[0]=2".
AFTER THAT u.ch[1] RUN AND IT print the value which is in SECOND byte. so we convert SECOND byte binary digit in decimal numbers and get 2. so "u.ch[0]=5".
And u.i run which means the whole memory takes by union so we convert 0000010100000010 into decimal numbers.which you know how convert binary to decimal.
And u.j also means the same
In that program first we assign u.ch[0]=3 which is array which index 0. so it takes memory in first byte and which is 00000011.
And then u.i=1;
Which REPLACE previous assigned memory of ch[0] and now it become 00000001
And then u.j=2;
Again REPLACE previous assigned memory of i and now it become 00000010
And now u.ch[1]=5 it takes memory in second byte and it is 00000101.
NOW IN MEMORY (00000101)(00000010)
second first byte
AND NOW printf("%d,%d,%d,%d",u.ch[0],u.ch[1],u.i,u.j); statement run.
IN THIS STATEMENT u.ch[0] is print the value which is in first byte. so we convert first byte binary digit in decimal numbers and get 2. so "u.ch[0]=2".
AFTER THAT u.ch[1] RUN AND IT print the value which is in SECOND byte. so we convert SECOND byte binary digit in decimal numbers and get 2. so "u.ch[0]=5".
And u.i run which means the whole memory takes by union so we convert 0000010100000010 into decimal numbers.which you know how convert binary to decimal.
And u.j also means the same
Kavya said:
1 decade ago
How we got the answer has 515? Try to expalin in detail how to find the union size using same fig used in explaining answer.
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
#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
Bhanu said:
1 decade ago
It gives different output in gcc or MS vc++.
In gcc the int size is 4 bytes. In the code only 2 bytes are initialized (MSB 2bytes contains uninitialized data). So output is garbage data. To avoid this add
u.i = 0;
u.ch[0] = 3;
u.ch[1] = 2;
In gcc the int size is 4 bytes. In the code only 2 bytes are initialized (MSB 2bytes contains uninitialized data). So output is garbage data. To avoid this add
u.i = 0;
u.ch[0] = 3;
u.ch[1] = 2;
Phani said:
1 decade ago
Hi nandini good explanation.
I broked my head for understanding. Your explanation helped me.
Thk you.
I broked my head for understanding. Your explanation helped me.
Thk you.
Rathika.b said:
1 decade ago
@nandhini:
I understood wat u told. but how 512 is got?
I understood wat u told. but how 512 is got?
Sameer said:
1 decade ago
@nandini.
Thanks a lot. Good explanation.
Thanks a lot. Good explanation.
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.
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.
Avinash said:
1 decade ago
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.i=11;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Try output for this program.
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.i=11;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Try output for this program.
Alisha said:
1 decade ago
Please explain how we get value of i?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers