读书人

关于union解决方案

发布时间: 2012-09-09 09:27:54 作者: rapoo

关于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;}/* 输出为:a971.35926e-043nan97*/


输出double时,为什么是nan?

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

读书人网 >C++

热点推荐