union

R

ramu

Hi,
main()
{
union {
float i;
int j;
} un;

un.i=2.35;
printf("%f\n",un.i);

un.j=3;
printf("%d\n",un.j);

printf("%f\n",un.i);
}

Am getting output as:
2.350000
3
0.000000

I wonder how it gives 0.000000 for the value of un.i when i printed its
value for the second time. can anyone help me out?

Regards
 
V

Vladimir S. Oka

ramu said:
Hi,
main()
{
union {
float i;
int j;
} un;

un.i=2.35;
printf("%f\n",un.i);

un.j=3;
printf("%d\n",un.j);

printf("%f\n",un.i);
}

Am getting output as:
2.350000
3
0.000000

I wonder how it gives 0.000000 for the value of un.i when i printed
its value for the second time. can anyone help me out?

In short (there was a more detailed discussion recently), all members of
an union share storage. If you think in terms of memory, they are all
stored in the same memory area (large enough to hold the largest union
member). Writing a value into one member, and then accessing another
will give implementation dependant results. On your implementation, it
seems that when you read a piece of storage, with integer 3 stored in
it, as a float, it happens you get a float 0.00000. You may have ended
up with an illegal value of the float as well, and run into all sorts
of problems. You were just lucky 9or knew _exactly_ waht you were
doing).

Cheers

Vladimir

PS
Don't get into habit of using floats. Use double instead.
 
R

ramu

Vladimir said:
In short (there was a more detailed discussion recently), all members of
an union share storage. If you think in terms of memory, they are all
stored in the same memory area (large enough to hold the largest union
member). Writing a value into one member, and then accessing another
will give implementation dependant results. On your implementation, it
seems that when you read a piece of storage, with integer 3 stored in
it, as a float, it happens you get a float 0.00000. You may have ended
up with an illegal value of the float as well, and run into all sorts
of problems. You were just lucky 9or knew _exactly_ waht you were
doing).

Cheers

Vladimir

PS
Don't get into habit of using floats. Use double instead.

--
Worst Vegetable of the Year:
The brussels sprout. This is also the worst vegetable of next
year.
-- Steve Rubenstein


Thanks a lot.

Will you please tell that why we have to use double instead of floats?

regards
 
R

Richard Heathfield

ramu said:
Hi,
main()
{
union {
float i;
int j;
} un;

un.i=2.35;
printf("%f\n",un.i);

un.j=3;
printf("%d\n",un.j);

printf("%f\n",un.i);
}

Am getting output as:
2.350000
3
0.000000

I wonder how it gives 0.000000 for the value of un.i when i printed its
value for the second time. can anyone help me out?

You can only store one value in a union. Your last store was to un.j, so the
current value is a float value. If you want the union to store more than
one value at the same time, you can do that using a special kind of union
known as a struct:

#include <stdio.h> /* required because a prototype is needed for the
variadic printf function */

int main(void) /* avoid dependence on implicit int */
{
struct {
float i;
int j;
} un;

un.i=2.35;
printf("%f\n",un.i);
un.j=3;
printf("%d\n",un.j);
printf("%f\n",un.i);

return 0; /* main returns int */
}

This program gives the output you expect.
 
M

Michael Mair

ramu said:
Vladimir S. Oka wrote:


Will you please tell that why we have to use double instead of floats?

float has fewer significant digits and you get inexact results
_much_ earlier. Usually, you cannot even store all long values
exactly in a float variable.
double has only slightly better guarantees with respect to
the number of digits but in practice has many more.
Using double thus means that you most of the time can forget
about precision (numerics and excessive financial calculation
excluded).
<OT> Apart from that, double is definitely not slower on modern
host PCs </OT>

Cheers
Michael
 
K

Keith Thompson

Vladimir S. Oka said:
In short (there was a more detailed discussion recently), all members of
an union share storage. If you think in terms of memory, they are all
stored in the same memory area (large enough to hold the largest union
member). Writing a value into one member, and then accessing another
will give implementation dependant results. On your implementation, it
seems that when you read a piece of storage, with integer 3 stored in
it, as a float, it happens you get a float 0.00000. You may have ended
up with an illegal value of the float as well, and run into all sorts
of problems. You were just lucky 9or knew _exactly_ waht you were
doing).

Actually, the value is (apparently) 0.00000 within the precision
displayed by printf's "%f" format. It's probably not exactly zero.
You might try using "%g" -- or "%.20g" if you want to see even more
digits.

Also, you need to add "#include <stdio.h>" (required if you're using
printf()), "main()" should be "int main(void)", and you should have a
"return 0;" at the end.
 
V

Vladimir S. Oka

Michael said:
float has fewer significant digits and you get inexact results
_much_ earlier. Usually, you cannot even store all long values
exactly in a float variable.
double has only slightly better guarantees with respect to
the number of digits but in practice has many more.
Using double thus means that you most of the time can forget
about precision (numerics and excessive financial calculation
excluded).
<OT> Apart from that, double is definitely not slower on modern
host PCs </OT>

Also, in many context in a C program, floats will be promoted/converted
to doubles anyway (and maybe then back to float again, if you assign
expression result to a float variable), thus negating any performance
gains you may think you got.

Cheers

Vladimir
 
M

Michael Mair

Vladimir said:
Also, in many context in a C program, floats will be promoted/converted
to doubles anyway (and maybe then back to float again, if you assign
expression result to a float variable), thus negating any performance
gains you may think you got.

Thanks -- I forgot to mention this; was in my head but did
not run through the fingers... :)

Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top