union的问题

ProgrammingRing 2011-10-06 10:38:26
例如如下代码:

#include <stdio.h>

int main(void)
{
union
{
float f;
int i;
} test;

test.f = 3.15;
printf("%d\n", test.i);
return 0;
}


#include <stdio.h>

int main(void)
{
union
{
char c;
int i;
} test;

test.c = 'a';
printf("%d\n", test.i);
return 0;
}

上面的两个例子中的test.i应该各自输出什么?输出结果是怎样得到的?
...全文
88 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
txzsp 2011-10-06
  • 打赏
  • 举报
回复
隐式转化输出整型
LixueDaddy 2011-10-06
  • 打赏
  • 举报
回复
学习了,呵呵。
浮尘grass 2011-10-06
  • 打赏
  • 举报
回复
同意一楼的
5t4rk 2011-10-06
  • 打赏
  • 举报
回复

隐式转化
星羽 2011-10-06
  • 打赏
  • 举报
回复
#include <stdio.h>

int main(void)
{
union
{
char c;
int i;
} test;

test.c = 'a';
printf("%d\n", test.i);
  相当于
printf("%d\n", *(int*)&test.c);
return 0;
}
星羽 2011-10-06
  • 打赏
  • 举报
回复
#include <stdio.h>

int main(void)
{
union
{
float f;
int i;
} test;

test.f = 3.15;
printf("%d\n", test.i);
  相当于
printf("%d\n", *(int*)&test.f);

return 0;
}

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧