union.c

来自「c21Examples.rar」· C语言 代码 · 共 33 行

C
33
字号
/* Example of using more than one union member at a time */
#include <stdio.h>

int main( void )
{
    union shared_tag {
        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;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?