Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > union

Reply
Thread Tools

union

 
 
ramu
Guest
Posts: n/a
 
      01-31-2006
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

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-31-2006
ramu wrote:

> 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.

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

 
Reply With Quote
 
 
 
 
ramu
Guest
Posts: n/a
 
      01-31-2006

Vladimir S. Oka wrote:
> ramu wrote:
>
> > 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.
>
> --
> Worst Vegetable of the Year:
> The brussels sprout. This is also the worst vegetable of next
> year.
> -- Steve Rubenstein



Thanks a lot.

>>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?

regards

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-31-2006
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.


--
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)
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      01-31-2006
ramu wrote:
> Vladimir S. Oka wrote:
>

<snip>

>>>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?


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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-31-2006
"Vladimir S. Oka" <> writes:
> ramu wrote:
>> 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).


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- <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.
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-31-2006
Michael Mair wrote:
> ramu wrote:
> > Vladimir S. Oka wrote:
> >

> <snip>
>
> >>>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?

>
> 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


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


 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      01-31-2006
Vladimir S. Oka wrote:
> Michael Mair wrote:
>
>>ramu wrote:
>>
>>>Vladimir S. Oka wrote:
>>>

>>
>><snip>
>>
>>>>>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?

>>
>>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.


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL UNION query in Java goli Java 4 04-05-2005 09:54 AM
SQL UNION -like filtering of offline DataSet into DataView for WebControl binding Marco Ippolito ASP .Net 2 05-19-2004 12:35 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
THE STATE OF THE UNION Jenna Bush MCSE 95 01-30-2004 02:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57