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;
}
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 1 of 4.
Vijay said:
3 years ago
Here the concept is union memory management. union memory size is largest data member.
Here
data members are : int i ; char a[2];
largest data member is int ie 4bytes.
note := 1byte=8bits => 1byte= 00000000.
4byte := 00000000 00000000 00000000 00000000.
for char u.a[0]=3; => uses first byte in memory
ie , 00000000 00000000 00000000 " 00000011 "
for char u.a[1]=1 ; => uses second byte(system allocates or selects like this).
ie , 00000000 00000000 "00000001" 00000011
whenever we are printing u.a[1] points to its 1 byte and gives 3
Similarly u.a[2] points to the second byte give 1.
when printing u.i, it is not initialized so it took the first 4 bytes ie entire memory
ie "00000000 00000000 00000001 00000011" => its value in binary is 515
2^8 + 2^1 + 2^0 =512+2+1 =515
So,u[i] prints 515.
Here
data members are : int i ; char a[2];
largest data member is int ie 4bytes.
note := 1byte=8bits => 1byte= 00000000.
4byte := 00000000 00000000 00000000 00000000.
for char u.a[0]=3; => uses first byte in memory
ie , 00000000 00000000 00000000 " 00000011 "
for char u.a[1]=1 ; => uses second byte(system allocates or selects like this).
ie , 00000000 00000000 "00000001" 00000011
whenever we are printing u.a[1] points to its 1 byte and gives 3
Similarly u.a[2] points to the second byte give 1.
when printing u.i, it is not initialized so it took the first 4 bytes ie entire memory
ie "00000000 00000000 00000001 00000011" => its value in binary is 515
2^8 + 2^1 + 2^0 =512+2+1 =515
So,u[i] prints 515.
(6)
Sree said:
1 decade ago
@Sai
The binary value of 2 is 00000010 and dat of 3 is 00000011...
Now (2)(3)---> 0000001000000011 = 2^9 + 2^1 + 2^0 = 515
The binary value of 2 is 00000010 and dat of 3 is 00000011...
Now (2)(3)---> 0000001000000011 = 2^9 + 2^1 + 2^0 = 515
(1)
Noel said:
7 years ago
Agree @Raju Kumar.
If I make the data types in the union ( int i; & char ch[2];) match (both int or both char, etc.) then the number of bytes match and I get a predictable and expected value for u.i. That is, it matches the value of what is in the ch[0] memory location.
If I make the data types in the union ( int i; & char ch[2];) match (both int or both char, etc.) then the number of bytes match and I get a predictable and expected value for u.i. That is, it matches the value of what is in the ch[0] memory location.
(1)
Bnr said:
9 years ago
Hear we are not assign any value to.
Int i; how to compiler assign 515 value.
Int i; how to compiler assign 515 value.
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).
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).
Rushabh said:
1 decade ago
But Why ch[0] is right side of ch[1]. Means why it is Right to left?
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?
1st reduce the value of A by 1 then find sum A and B then incriment B by 1?
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?
u.ch[1]=2; for calculating (u.i) why we are taking 2 as first and 3 as second value?
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.
{
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.
Sabi said:
1 decade ago
I can't understand that. How 512 is came?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers