首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于union解决方案

2012-09-09 
关于unionC/C++ code#include iostreamusing namespace stdunion Prac{char chint ifloat fdouble d

关于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标准,所以你这个其实没什么意义,编程中不要使用。

热点排行