Thread: How to calculate value of one member of union when assigning a value to other one?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    Question How to calculate value of one member of union when assigning a value to other one?

    We are assigning 100 to member integer x of value as defined type union number in the following program. Then we are printing both members, and we are seeing that value.y has a value due to union.

    When I ran this program, I saw the following:

    How to calculate value of one member of union when assigning a value to other one?-output_union-jpg


    According to that members of union share the same storage space;

    1-) Is the size of storage 4bytes(int) + 8bytes(double) = 12bytes = 96bits? Or is it just 8bytes(double) = 64bits?

    2-) Why is member x equal to 0(zero) when assigning any data to member y?

    3-) Can we calculate member y when assigning any data to member x? If It can be done, how? How can we know where bits of value.x and value.y in the 96bits-field or 64bits-field?

    4-) In my study book, when assigning 100 to member x, the output of member y is showed as -92559592117433136000000000000000000000000000000000 0000000000000.000000 So why doesn't the values in member y in which the picture above and my study book have the same value?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    union number {
        int x;
        double y;
    };
    
    
    int main()
    {
        union number value;
        value.x = 100;
        printf("Put a value in the integer member\n"
               "and print both members.\n\n"
               "int:    %d\n\n"
               "double: %f\n\n\n\n",value.x,value.y);
        value.y = 100.0;
        printf("Put a value in the floating member\n"
              "and print both members.\n\n"
              "int:    %d\n\n"
              "double: %f\n\n\n\n",value.x,value.y);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1-) Is the size of storage 4bytes(int) + 8bytes(double) = 12bytes = 96bits? Or is it just 8bytes(double) = 64bits?
    Print the value of sizeof(value)

    2-) Why is member x equal to 0(zero) when assigning any data to member y?
    Luck?
    You can't infer anything about .x when assigning to .y (or vice-versa).

    3-) Can we calculate member y when assigning any data to member x? If It can be done, how? How can we know where bits of value.x and value.y in the 96bits-field or 64bits-field?
    Well if y has more bits than x, then assigning x will leave some bits of y undefined.


    4-) In my study book, when assigning 100 to member x, the output of member y is showed as -92559592117433136000000000000000000000000000000000 0000000000000.000000 So why doesn't the values in member y in which the picture above and my study book have the same value?
    Because what you're doing is undefined.
    Meaning that anything you see is only significant to you for that moment in time.
    The book is wrong if it implies that this is always the answer (a sign perhaps to get another book).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    I thought that assigning value to members of a union are performed in order on the basis of the memory. Thank you for your answers, especially for first answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct as a Union member
    By csharp100 in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2012, 08:38 PM
  2. Replies: 2
    Last Post: 02-04-2011, 10:07 AM
  3. Replies: 4
    Last Post: 09-04-2010, 09:12 AM
  4. Replies: 1
    Last Post: 05-18-2010, 04:14 AM
  5. Replies: 2
    Last Post: 03-17-2008, 12:21 AM

Tags for this Thread