Thread: Couldn't understand output of union initialized members!!

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    Couldn't understand output of union initialized members!!

    Code:
    //Structures and unions 
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char const *argv[])
    {
        union sample 
        {
            int m;
            float n;
            char ch;
        }u;
    
    
        system("clear");
    
    
        printf("Size of union = %lu bytes\n", sizeof(u));
        /*initializes members*/
    
    
        u.m = 25;
    
    
        printf("%d %-4.2f %c\n", u.m, u.n, u.ch);
    
    
        u.n = 0.2;
    
    
        printf("%d %-4.2f %c\n", u.m, u.n, u.ch);
    
    
        u.ch     = 'b';
    
    
        printf("%d %-4.2f %c\n", u.m, u.n, u.ch);
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    what is wrong with it?
    Code:
    Size of union = 4 bytes
    u.m = 25 0.00  
    u.n = 1045220557 0.20 
    u.ch = 1045220450 0.20 b
    VS. what are you telling it to print out for each value using your printf specifiers against its data type. What were you expecting to see?

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    28
    What's the reason of the number 1045***

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    The best way to understand what is happening is by looking at the binary representation of each number. A union doesn't work like a struct in that all of the union members overlap each other in memory. This means that when you assign a value to one union member, that value gets assigned to all of them. What is making your output change from one value to another is the format specifiers in printf rather than the values stored in your union. I could write you a bit mask program to show you the bits of the variables, but you could get the same effect by typing in the values on this website as well.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    It is very possible that I became so wrapped up in coding this today that I can't think clearly enough to find a way to apply the program I wrote to the suggested problem. The union members are not working as I had expected. The program is able to change from a variable to its bit representation and from a bit representation to a variable for both floats and ints. I had expected this to be able to show how unions work.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
     
    union sample {
        int m;
        float n;
        char ch;
    } u;
     
    char* intBits(int x, int mode);
    int bitsInt(char buffer[37]);
    char* floatBits(double x, int mode);
    float bitsFloat(char buffer[37]);
    char* spaceBuffer(char buffer[37]);
     
    int main(void) {
        
        printf("Size of union = %lu bytes\n", sizeof(u));
        printf("Size of int = %d bytes\n", sizeof(int));
        printf("Size of float = %d bytes\n", sizeof(float));
        puts("");
     
        printf("int test: %d\n", bitsInt(intBits(25, 1)));
        printf("bitsFloat test : %lf\n", bitsFloat(floatBits(45.45, 1)));
        puts("");
     
        u.m = 25;
        printf("%10s = %s\n", "u.m int", intBits(u.m, 0));
        printf("%10s = %s\n", "u.m float", floatBits(u.m, 0));
        puts("");
     
        u.n = (float)45.45;
        printf("%10s = %s\n", "u.m int", intBits(u.m, 0));
        printf("%10s = %s\n", "u.m float", floatBits(u.n, 0));
        puts("");
     
        u.m = 26;
        printf("%10s = %d\n", "u.m int", u.m);
        printf("%10s = %lf\n", "u.m float", u.n);
        puts("");
     
        u.m = 84;
        printf("%10s = %d\n", "u.m int", u.m);
        printf("%10s = %lf\n", "u.m float", u.n);
        puts("");
     
        
    }
     
    // if mode == 0, bit output will be formatted with spaces
     
    // changes an int into bits
    char* intBits(int x, int mode) {
        // 8 bits per byte at 4 bytes plus 4 spaces and a '\0' is 37 characters
        static char buffer[37];
        memset(buffer, 0, sizeof(buffer));
     
        unsigned int mask = 1 << 31;
        for (unsigned int i = 0; i < 32; ++i) {
            sprintf(&buffer[i], "%c", x & mask ? '1' : '0');
            x <<= 1;
        }
     
        if (mode == 0) {
            spaceBuffer(buffer);
        }
        return buffer;
    }
     
    // changes bits into an int
    int bitsInt(char buffer[37]) {
        return (int) strtol(buffer, NULL, 2);
    }
     
    // changes float into bits
    char* floatBits(double x, int mode) {
        static char buffer[37];
        memset(buffer, 0, sizeof(buffer));
        
        // 1 sign bit
        if (x < 0) {
            sprintf(buffer, "%c", '1');
            x *= -1;
        }
        else {
            sprintf(buffer, "%c", '0');
        }
     
        // 8 exponent bits
        int whole = (int)floor(x);
        char wholeBits[100];
        strcpy(wholeBits, intBits(whole, 1));
        int wholeBitsSize = 0;
        while (wholeBits[wholeBitsSize] != '1') {
            ++wholeBitsSize;
        }
        ++wholeBitsSize;
        int exponent = 32 - wholeBitsSize + 127;
        strcpy(&buffer[1], &intBits(exponent, 1)[24]);
     
        // 23 mantissa bits = whole bits + fractional bits
        strcpy(&buffer[9], &wholeBits[wholeBitsSize]);
     
        double fractional = x - floor(x);
        for (int i = exponent - 127; i < 23; ++i) {
            fractional *= 2;
            if (fractional > 1) {
                buffer[9 + i] = '1';
                fractional -= 1;
            }
            else {
                buffer[9 + i] = '0';
            }
        }
        if (mode == 0) {
            spaceBuffer(buffer);
        }
        return buffer;
    }
     
    // changes bits into a float
    float bitsFloat(char buffer[37]) {
        
        // 1 sign bit
        int sign = 1;
        if (buffer[0] == '1') {
            sign *= -1;
        }
     
        // 8 exponent bits
        char expBuffer[8];
        strncpy(expBuffer, &buffer[1], 8);
        int exponent = strtol(expBuffer, NULL, 2) - 127;
     
        // 23 matissa bits = whole bits + fractional bits
        char wholeBuffer[100];
        if (exponent > 0) {
            wholeBuffer[0] = '1';
        }
        else {
            wholeBuffer[0] = '0';
        }
        
        strncpy(&wholeBuffer[1], &buffer[9], exponent);
        int whole = strtol(wholeBuffer, NULL, 2);
     
        float fractional = 0;
        int mult = 2;
        for (int i = exponent; i < 23; ++i) {
            if (buffer[9 + i] == '1') {
                fractional += (float)1 / mult;
            }
            mult *= 2;
        }
     
        fractional += whole;
        fractional *= sign;
        
        return fractional;
    }
     
    // provides bit space formating for readability
    char* spaceBuffer(char buffer[37]) {
        char tempBuffer[100];
        for (int i = 4; i < 37; i += 5) {
            strcpy(tempBuffer, &buffer[i]);
            buffer[i] = ' ';
            strcpy(&buffer[i + 1], tempBuffer);
        }
        return buffer;
    }
    Output:
    Code:
    Size of union = 4 bytes
    Size of int = 4 bytes
    Size of float = 4 bytes
    
    int test: 25
    bitsFloat test : 45.449997
    
       u.m int = 0000 0000 0000 0000 0000 0000 0001 1001
     u.m float = 0100 0001 1100 1000 0000 0000 0000 0000
    
       u.m int = 0100 0010 0011 0101 1100 1100 1100 1101
     u.m float = 0100 0010 0011 0101 1100 1100 1100 1100
    
       u.m int = 26
     u.m float = 0.000000
    
       u.m int = 84
     u.m float = 0.000000
    Last edited by jack jordan; 11-09-2017 at 02:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-29-2011, 08:41 PM
  2. Static array of initialized members?
    By Blackroot in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2009, 09:36 PM
  3. i cant understand why i get such an output??
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 10-16-2008, 06:06 AM
  4. program that i couldn't understand
    By elton_fan in forum C Programming
    Replies: 9
    Last Post: 03-24-2007, 03:18 PM
  5. wrong output in struct cum union example
    By bhagwat_maimt in forum C Programming
    Replies: 5
    Last Post: 01-10-2007, 11:23 AM

Tags for this Thread