Using more than one union member at a time - C Data Type

C examples for Data Type:union

Description

Using more than one union member at a time

Demo Code

#include <stdio.h>

int main( void ){
    union flag {/* ww  w .  j  a v a2 s .c o m*/
        char   c;
        int    i;
        long   l;
        float  f;
        double d;
    } shared;

    shared.c = '$';

    printf("\nchar c   = %c",  shared.c);
    printf("\nint i    = %d",  shared.i);
    printf("\nlong l   = %ld", shared.l);
    printf("\nfloat f  = %f",  shared.f);
    printf("\ndouble d = %f",  shared.d);

    shared.d = 123456789.8765;

    printf("\n\nchar c   = %c",  shared.c);
    printf("\nint i    = %d",  shared.i);
    printf("\nlong l   = %ld", shared.l);
    printf("\nfloat f  = %f",  shared.f);
    printf("\ndouble d = %f\n",  shared.d);

    return 0;
}

Result


Related Tutorials