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 3 of 8.
Komal said:
1 decade ago
ch is char type of variable then how can we assign it number?
Ritika said:
1 decade ago
Please explain in detail why is it giving 515 as the size of the union?
Preethi said:
8 years ago
Why we find the size of the union in coding doesn't mention about size only the variable value?
Jayakumar v said:
4 years ago
Good explanation. Thanks, everyone.
Mesi said:
6 years ago
What will be the result if we add one more value u.ch[2]=4?
Mahesh said:
6 years ago
How that I and other two char arrays are related ?
We are asked to find I and how we are relating it with other variables.
We are asked to find I and how we are relating it with other variables.
Harika said:
6 years ago
Can anyone explain the memory allocation?
Please.
Please.
Saila said:
6 years ago
Thank you @Mostafa Hamoda.
Ajay gupta said:
7 years ago
@All.
As explained that how it became 515 and I understood it. But why we need to calculate the size of the union while we are accessing the value of the variable I in the union u. why the size and how u, I is related to the size.
As explained that how it became 515 and I understood it. But why we need to calculate the size of the union while we are accessing the value of the variable I in the union u. why the size and how u, I is related to the size.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers