C program to declare, initialize an union, example of union

In this program, we will learn to declare, initialize a union in C programming languages, how to assign the values to union elements and how to access assigned values through union object?

Example of creating, initializing a union in C

/*C program to declare, initialize a UNION, 
example of UNION*/

#include <stdio.h>

// union declaration
union pack {
    char a;
    int b;
    double c;
};

int main()
{
    pack p; //union object/variable declaration

    printf("\nOccupied size by union pack: %d", sizeof(pack));

    // assign value to each member one by one other it will replace last value
    p.a = 'A';
    printf("\nValue of a:%c", p.a);

    p.b = 10;
    printf("\nValue of b:%d", p.b);

    p.c = 12345.6790;
    printf("\nValue of c:%f", p.c);

    // see, what will happen? if u will assign values together
    p.a = 'A';
    p.b = 10;
    p.c = 12345.6790;

    // here the last value of p.c will be accessed by all members
    printf("\nValue of a:%c, b:%d, c:%f", p.a, p.b, p.c);

    return 0;
}

Output

Occupied size by union pack: 8
Value of a:A
Value of b:10 
Value of c:12345.679000 
Value of a:�, b:-377957122, c:12345.679000

C Structure & Union Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.