printing all variables in union

R

rahul8143

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]);
}
 
K

Keith Thompson

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.
 
R

rahul8143

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
 
R

rahul8143

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
 
C

Cong Wang

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]);
}
That all depends on your compiler and machine.Using Microsoft compiler
is NOT a good choice!Try gcc instead.:)
 
M

manoj1978

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]);
}
your printf have extra arguments,printf take one format string only.
use printf("%d %d %d\n",obj.i,obj.ch[0],obj.ch[1]);
 
M

manoj1978

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]);
}
your printf have extra arguments,printf take one format string only.
use printf("%d %d %d\n",obj.i,obj.ch[0],obj.ch[1]);

forgot to add,since INTEL is little endian, for 256 (0x0100) o/p will
be 256 0 1
 
K

Keith Thompson

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

Please provide some context when you post a followup. Don't assume
that your readers can see the article to which you're replying.

As we have said here literally hundreds of times:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

As for the output of the program, the code you posted is buggy. The
call to printf is incorrect. If the code you posted is actually the
same as what's in the written test, you should complain to whoever
wrote the test.

Speaking of which, if it was a written test for a class you're taking,
we're not going to do your homework for you.
 
J

junky_fellow

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]);
}

The above program did not compile on my machine.
I assume you are trying to print obj.i, obj.ch[0] and obj.ch[1]
as signed integers.
The answer on INTEL (assuming it to be little endian) machine
would be:
256 0 1.

Reason: integer 256=0x100 will be stored on a little endian machine
(assuming size of int to be 4)
as 00 01 00 00
 
R

ranjeet.gupta

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]);
}

<OT wrt OP question>
JUST TO ADD

Please refer the below link, As most of the guys has already solved
your problem. Hope this may more clear your doubts and
enrich your concept more.

http://groups.google.com.sg/group/c...t&q=union+issue&rnum=1&hl=en#41e32db89644b906


Regards
Ranjeet
 
S

Steven Kobes

hello,
what will be output of following program on INTEL machine

If the answer depends on the fact that it's an Intel machine, then
it's probably off-topic here.
#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]);
}

This program will probably print:
256

But it has undefined behavior due to (1) void main and (2) accessing a
union member other than the most recently written one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top