联合打印所有变量 [英] printing all variables in union

查看:56
本文介绍了联合打印所有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

将在INTEL机器上输出以下程序

#include< stdio.h>


void main()

{

union s

{

int i;

char ch [2];

};

union s obj;

obj.i = 256;

printf("%d \ n",obj.i,"%d",obj.ch [0],"%d",obj.ch [1]);

}

解决方案

ra *******@gmail.com 写道:

你好,
将在INTEL机器上输出以下程序
#include< stdio .h>

void main()
{
结合s
{i /;
char ch [2];
};
union s obj;
obj.i = 256;
printf("%d \ n",obj.i,"%d",obj.ch [0],%d,obj.ch [1]);
}




因为你声明main为返回void,而不是正确

返回int类型,它调用未定义的行为(除非你使用

一个特别接受void main()的实现)。如果您已经关注此新闻组,或者您已阅读常见问题解答,那么您将知道这一点。


假设你的实现接受void main(),任何

系统的输出都是字符串256。然后换行。如果您在发布之前已经尝试运行该程序,那么你会知道

,尽管你可能还不明白为什么。阅读你的

教科书或系统的文档,了解

printf()函数是如何工作的(提示:它需要一个单独的格式字符串)。 />

我怀疑你也假设一个int是两个字节。它可能是,b $ b但它通常不是。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

San迭戈超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


你好kieth,

好​​吧,让我告诉你我在微软上运行这个程序

Visual C ++ 6.0编译器,只有256个答案,没有任何错误或

警告。

这不是我的程序,但它在书面测试中问我/>
答案为

256 0 0

256 0 1

256 128 128

但我没有得到任何上述内容所以我在这里提问。

任何人都可以帮助我


我错了在上一篇文章中说明答案选项。鉴于3

答案不是答案,而是将它们视为可供选择的选项

。他们只有3个选择中的一个答案

256 0 0

256 0 1

256 128 128


hello,
what will be output of following program on INTEL machine
#include <stdio.h>

void main()
{
union s
{
int i;
char ch[2];
};
union s obj;
obj.i=256;
printf("%d\n",obj.i,"%d",obj.ch[0],"%d",obj.ch[1]);
}

解决方案

ra*******@gmail.com writes:

hello,
what will be output of following program on INTEL machine
#include <stdio.h>

void main()
{
union s
{
int i;
char ch[2];
};
union s obj;
obj.i=256;
printf("%d\n",obj.i,"%d",obj.ch[0],"%d",obj.ch[1]);
}



Since you declared main as returning void, rather than the correct
return type of int, it invokes undefined behavior (unless you''re using
an implementation that specifically accepts void main()). If you had
been following this newsgroup, or if you had read the FAQ, you would
know that.

Assuming your implementation accepts "void main()", the output on any
system will be the string "256" followed by a newline. If you had
actually tried running the program before posting it, you would know
that as well, though you might not yet understand why. Read your
textbook, or your system''s documentation, to understand how the
printf() function works (hint: it takes a single format string).

I suspect you''re also assuming that an int is two bytes. It may be,
but it commonly isn''t.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


hello kieth,
ok fine let me tell you that i run this program on Microsoft
Visual C++ 6.0 compiler and got only 256 answer without any error or
warning.
This is not mine program but its asked me in written test and
answers are given as
256 0 0
256 0 1
256 128 128
but i am not getting any of above so i ask question here.
can anybody help me


i did mistake in stating answer options in previous post. Given 3
answers are not answers rather consider them as choices to be selected
from them. thers only one answer from 3 choices
256 0 0
256 0 1
256 128 128


这篇关于联合打印所有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆