主题:共用体变量使用出错~
以下为我做的一例题的代码:
#include <stdio.h>
union number
{
int x;
float y;
};
main()
{
union number value; /*声明类型为union number的变量value*/
value.x=100; /*为value的元素x赋值*/
printf("%s\n%s\n%s%d\n%s%f\n\n",
"Put a value in the integer member",
"and print both members.",
"int:",value.x,
"float:",value.y);
value.y=12; /*为value的元素y赋值*/
printf("%s\n%s\n%s%d\n%s%f\n\n",
"Put a value in the floating member",
"and print both members.",
"int:",value.x,
"float:",value.y);
getch();
}
结果,为图片附件,我已经在VS2008,VC6.0,中都试过了,
为什么第二次输出时,value中x输出为一个其他数,不为0(书上的结果为0)~
#include <stdio.h>
union number
{
int x;
float y;
};
main()
{
union number value; /*声明类型为union number的变量value*/
value.x=100; /*为value的元素x赋值*/
printf("%s\n%s\n%s%d\n%s%f\n\n",
"Put a value in the integer member",
"and print both members.",
"int:",value.x,
"float:",value.y);
value.y=12; /*为value的元素y赋值*/
printf("%s\n%s\n%s%d\n%s%f\n\n",
"Put a value in the floating member",
"and print both members.",
"int:",value.x,
"float:",value.y);
getch();
}
结果,为图片附件,我已经在VS2008,VC6.0,中都试过了,
为什么第二次输出时,value中x输出为一个其他数,不为0(书上的结果为0)~