Connecting Tech Pros Worldwide Help | Site Map

union

ramu
Guest
 
Posts: n/a
#1: Jan 31 '06
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

Vladimir S. Oka
Guest
 
Posts: n/a
#2: Jan 31 '06

re: union


ramu wrote:
[color=blue]
> 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?[/color]

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

ramu
Guest
 
Posts: n/a
#3: Jan 31 '06

re: union



Vladimir S. Oka wrote:[color=blue]
> ramu wrote:
>[color=green]
> > 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?[/color]
>
> 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[/color]


Thanks a lot.
[color=blue][color=green]
>>PS
> >Don't get into habit of using floats. Use double instead.[/color][/color]

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

regards

Richard Heathfield
Guest
 
Posts: n/a
#4: Jan 31 '06

re: union


ramu said:
[color=blue]
> 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?[/color]

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.


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Michael Mair
Guest
 
Posts: n/a
#5: Jan 31 '06

re: union


ramu wrote:[color=blue]
> Vladimir S. Oka wrote:
>[/color]
<snip>
[color=blue][color=green][color=darkred]
>>>PS
>>>Don't get into habit of using floats. Use double instead.[/color][/color]
>
> Will you please tell that why we have to use double instead of floats?[/color]

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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Keith Thompson
Guest
 
Posts: n/a
#6: Jan 31 '06

re: union


"Vladimir S. Oka" <novine@btopenworld.com> writes:[color=blue]
> ramu wrote:[color=green]
>> 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?[/color]
>
> 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).[/color]

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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Vladimir S. Oka
Guest
 
Posts: n/a
#7: Jan 31 '06

re: union


Michael Mair wrote:[color=blue]
> ramu wrote:[color=green]
> > Vladimir S. Oka wrote:
> >[/color]
> <snip>
>[color=green][color=darkred]
> >>>PS
> >>>Don't get into habit of using floats. Use double instead.[/color]
> >
> > Will you please tell that why we have to use double instead of floats?[/color]
>
> 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>[/color]

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

[color=blue]
> Cheers
> Michael
> --
> E-Mail: Mine is an /at/ gmx /dot/ de address.[/color]

Michael Mair
Guest
 
Posts: n/a
#8: Jan 31 '06

re: union


Vladimir S. Oka wrote:[color=blue]
> Michael Mair wrote:
>[color=green]
>>ramu wrote:
>>[color=darkred]
>>>Vladimir S. Oka wrote:
>>>[/color]
>>
>><snip>
>>[color=darkred]
>>>>>PS
>>>>>Don't get into habit of using floats. Use double instead.
>>>
>>>Will you please tell that why we have to use double instead of floats?[/color]
>>
>>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>[/color]
>
>
> 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.[/color]

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

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Closed Thread