union

Discussion in 'C Programming' started by ramu, Jan 31, 2006.

  1. ramu

    ramu Guest

    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
     
    ramu, Jan 31, 2006
    #1
    1. Advertising

  2. 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
     
    Vladimir S. Oka, Jan 31, 2006
    #2
    1. Advertising

  3. ramu

    ramu Guest

    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
     
    ramu, Jan 31, 2006
    #3
  4. 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)
     
    Richard Heathfield, Jan 31, 2006
    #4
  5. ramu

    Michael Mair Guest

    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.
     
    Michael Mair, Jan 31, 2006
    #5
  6. "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) <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.
     
    Keith Thompson, Jan 31, 2006
    #6
  7. 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.
     
    Vladimir S. Oka, Jan 31, 2006
    #7
  8. ramu

    Michael Mair Guest

    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.
     
    Michael Mair, Jan 31, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco Ippolito
    Replies:
    2
    Views:
    3,206
    Marco Ippolito
    May 19, 2004
  2. goli

    SQL UNION query in Java

    goli, Apr 4, 2005, in forum: Java
    Replies:
    4
    Views:
    2,607
  3. =?ISO-8859-1?Q?Thomas_G=E4rtner?=

    JNI working with jvalue (union)

    =?ISO-8859-1?Q?Thomas_G=E4rtner?=, Sep 19, 2005, in forum: Java
    Replies:
    3
    Views:
    3,384
    Roedy Green
    Sep 20, 2005
  4. Matt Garman
    Replies:
    1
    Views:
    671
    Matt Garman
    Apr 25, 2004
  5. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    877
    Chris Torek
    Apr 26, 2004
Loading...

Share This Page