Re: printing all variables in union
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 25 Jul 2005 04:08:52 GMT
rahul8143@xxxxxxxxx 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) kst-u@xxxxxxx <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.
.
- Follow-Ups:
- Re: printing all variables in union
- From: rahul8143
- Re: printing all variables in union
- References:
- printing all variables in union
- From: rahul8143
- printing all variables in union
- Prev by Date: Re: Should function argument be changed in function body?
- Next by Date: Re: Should function argument be changed in function body?
- Previous by thread: printing all variables in union
- Next by thread: Re: printing all variables in union
- Index(es):
Relevant Pages
|