Re: union
- From: "ramu" <ramu.ask@xxxxxxxxx>
- Date: 30 Jan 2006 23:47:44 -0800
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
.
- Follow-Ups:
- Re: union
- From: Michael Mair
- Re: union
- Prev by Date: Re: What u mean by this statemnet ?.
- Next by Date: Re: union
- Previous by thread: Re: union
- Next by thread: Re: union
- Index(es):
Relevant Pages
|