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 3 of 4.
Bittu said:
9 years ago
Union uses Shared memory.
So, char[] and int i occupy the same memory location.
So, char[] and int i occupy the same memory location.
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.
Vineeta said:
4 years ago
Please explain me, why use 8 binary bits 00000011?
Anyone explain about it.
Anyone explain about it.
Ramya said:
1 decade ago
hey i cant get ur answer ,plz explain me how the value of u.i is assigned
Rushabh said:
1 decade ago
But Why ch[0] is right side of ch[1]. Means why it is Right to left?
Priyu said:
1 decade ago
@ ashwni
In ashish question the value of the u.i should be 515?
In ashish question the value of the u.i should be 515?
Bhanu said:
1 decade ago
How you get that value? please give explain in detail.
Pranay said:
1 decade ago
Union takes memory, according the largest data types.
SATHYA said:
1 decade ago
@Sundar
Your explanation is the best one. THANKS.
Your explanation is the best one. THANKS.
Supraja said:
7 years ago
Here can we take union u instead of union a u?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers