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 5 of 8.
Ravi said:
1 decade ago
Why we are assigning the 515 to the int i?
Why 'i' is not having any other value(like 0 or 1)?
Why 'i' is not having any other value(like 0 or 1)?
Naveen said:
1 decade ago
@Suman sagar.
Consider it has a memory cell.
These are 2 memory cell(1byte =8 bits) ,size of the union is 2memory cells u.ch[0] occupies 1st memory cell ie 1-128, u.ch[1] occupies 2nd memory cell ie 256-65536. value assigned to u.ch[0] is 3 indicate the in 1st memory cell, u.ch[1] is assigned 2 indicate that is 2nd memory cell look for u.ch[1] it is like starting 1-128 but 1-65536 is one memory cell as a whole for the union.
So according to u.ch[1] its value is 2.
But according to u[i] its value is 512 + 2+ 1.
Int occupies two memory cells whereas char occupies 1 memory cell.
Consider it has a memory cell.
These are 2 memory cell(1byte =8 bits) ,size of the union is 2memory cells u.ch[0] occupies 1st memory cell ie 1-128, u.ch[1] occupies 2nd memory cell ie 256-65536. value assigned to u.ch[0] is 3 indicate the in 1st memory cell, u.ch[1] is assigned 2 indicate that is 2nd memory cell look for u.ch[1] it is like starting 1-128 but 1-65536 is one memory cell as a whole for the union.
u.ch[2] view 128 64 32 .... 2 1 | 128 .. 4 2 1
u[i] view 65536 .. 1024 512 256 | 128 64.. 2 1
1 0 1 1
So according to u.ch[1] its value is 2.
But according to u[i] its value is 512 + 2+ 1.
Int occupies two memory cells whereas char occupies 1 memory cell.
Suman sagar said:
1 decade ago
Actually union size is the biggest size of its variable. But by using the instance of union if we access u.i it will indicate the value of union member/variable. But why it is giving the answer as 515?
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?
{
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?
Neha said:
1 decade ago
I have a doubt that structure and union is almost same then why do we need union in first place if structure is already there? I know the difference between them but don't know the purpose of using union over structure? please anyone?
Srujan reddy said:
1 decade ago
%d is a format specifier for integer. So when u use %d in printf() statement for a character it will print the ascii value of that character? Explain in detail how could the solution gain those output value.
union has two variables indeed. the one is an integer and a character sequence of 2.
So memory is allocated either for int variable or for character sequence. but could you access the all the variables of the union at a time?
union has two variables indeed. the one is an integer and a character sequence of 2.
So memory is allocated either for int variable or for character sequence. but could you access the all the variables of the union at a time?
Swetha92 said:
1 decade ago
I tried out this
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.ch[2] = 7;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
I got the ans as 3,2,7,459267 and I also verified manually
But can u explain me why when char[2] declaration changed to char[3] gives result as 3,2,7,134676995
I mean I get the above result for the following program
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[3];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.ch[2] = 7;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
What does char[2] and char[3] declarations actually do to the program?
Pls explain me.
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.ch[2] = 7;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
I got the ans as 3,2,7,459267 and I also verified manually
But can u explain me why when char[2] declaration changed to char[3] gives result as 3,2,7,134676995
I mean I get the above result for the following program
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[3];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.ch[2] = 7;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
What does char[2] and char[3] declarations actually do to the program?
Pls explain me.
Hemant7298 said:
1 decade ago
Thanks Soma.
Jay Patel said:
1 decade ago
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.
Now we can call the size of union i.e, 00000010 00000011.
See the fig so 1*1+2*1+512*1=515.
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers