Sail E0 Webinar
Question

What will be output if you compile following c code ?


#include<stdio.h>
union A
{
char ch;
int i;
float f;
}tempA;
void main()
{
tempA.ch='A';
tempA.i=777;
tempA.f=12345.12345;
printf("%d",tempA.i);
}


Options:
A .  compile error
B .  12345
C .  777
D .  Erroneous Output
Answer: Option D

The above program produces erroneous output (which is machine dependent). In effect, a union creates a storage location that can be used by any one of its members at a time. When a different member is assigned a new value, the new value supercedes the previous member's value.
[NOTE : The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union i.e. all members share the same address.]



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers