关于union解决方案

关于union
C/C++ code


#include <iostream>

using namespace std;

union Prac
{
    char ch;
    int i;
    float f;
    double d;
};

int main()
{
    Prac u1;
    u1.ch = 'a';
    cout << u1.ch << endl;
    cout << u1.i << endl;
    cout << u1.f << endl;
    cout << u1.d << endl;
    cout << u1.i << endl;

    return 0;
}

/* 输出为:
a
97
1.35926e-043
nan
97
*/



输出double时,为什么是nan?

------解决方案--------------------
a
-858993567
-1.07373e+008
-9.25596e+061
-858993567
我的输出~int类型要看CPU大端小端,float和double要考虑IEEE标准,所以你这个其实没什么意义,编程中不要使用。