You are here:

C/Union undefined variables default value

Advertisement


Question
Hi,
Can u explain me the output of the following code?

#include <stdio.h>
void main ()
{
union {
int i;
float f;
char ch[5];
}u;
printf ("Value of float %f, %u\n", u.f, &u.f);
}

If I read float in the above union what is the output?
Output is:0.000000 and its address.
But how and why?  

Answer
For any variable (except global variables), the value is undefined when it is defined.
You must initialize it before using.
So, I cannot tell anything about the o/p, since you are dealing with an undefined (sometimes called garbage) value.
The same may give different o/p on different computers!

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.