Union in C

Algorithm

Step 1: Start

Step 2: Declare a union with int, float, and char members

Step 3: Assign a character value to the union

Step 4: Assign an integer value to the union (overwrites previous)

Step 5: Assign a float value to the union (overwrites previous)

Step 6: Print all members of the union

Step 7: End

Flowchart

graph TD; A([Start]) --> B[Declare union]; B --> C[Assign char value]; C --> D[Assign int value]; D --> E[Assign float value]; E --> F[/Print all values/]; F --> G([End]); classDef start fill:#00cc44,stroke:#006622,stroke-width:2px,color:#ffffff; classDef stop fill:#ff4444,stroke:#cc0000,stroke-width:2px,color:#ffffff; classDef process fill:#4488ff,stroke:#0044cc,stroke-width:2px,color:#ffffff; classDef input_output fill:#ffdd44,stroke:#cc9900,stroke-width:2px,color:#000000; class A start; class G stop; class B,C,D,E process; class F input_output;

Program

#include <stdio.h>

union Number {
    int intValue;
    float floatValue;
    char charValue;
};

int main() {
    union Number num;

    num.charValue = 'A';
    num.intValue = 10;
    num.floatValue = 3.14;

    printf("Char value: %c\n", num.charValue);
    printf("Integer value: %d\n", num.intValue);
    printf("Float value: %.2f\n", num.floatValue);

    return 0;
}
                

Test Case Output
Char value: ���
Integer value: 1078523331
Float value: 3.14