473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

printing all variables in union

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

Nov 15 '05 #1
14 4469
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.
Nov 15 '05 #2
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

Nov 15 '05 #3
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

Nov 15 '05 #4


ra*******@gmail.com wrote:
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.:-)

Nov 15 '05 #5


ra*******@gmail.com wrote:
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]);

Nov 15 '05 #6


ma*******@gmail.com wrote:
ra*******@gmail.com wrote:
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

Nov 15 '05 #7
hello congwang,
where can i get gcc compiler for windows?

Nov 15 '05 #8
ra*******@gmail.com writes:
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.

--
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.
Nov 15 '05 #9


ra*******@gmail.com wrote:
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

Nov 15 '05 #10


ra*******@gmail.com wrote:
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/co...e32db89644b906
Regards
Ranjeet

Nov 15 '05 #11
Rahul wrote:
where can i get gcc compiler for windows?


http://www.cygwin.com/setup.exe
Nov 15 '05 #12


ra*******@gmail.com wrote:
hello congwang,
where can i get gcc compiler for windows?

A lot!Both DJGPP and CYGWIN are available on Internet and are
free.Enjoy them!

Nov 15 '05 #13
ra*******@gmail.com wrote:
hello,
what will be output of following program on INTEL machine
#include <stdio.h>

void main()

^^^^
anything
Nov 15 '05 #14
ra*******@gmail.com wrote:
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.
Nov 15 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: TinTin | last post by:
Hello, How do I concatinate a variable. Here's the scenarios: declare @var1 varchar(20) declare @var2 varchar(20) declare @var3 varchar(20) declare @var4 varchar(20) .. ..
6
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having...
2
by: Hexman | last post by:
Hello All, I'm now getting into the reporting phase of my application development. I need to print not only continuous page reports from a datasource (database, datagridview, listboxes, etc),...
16
by: junky_fellow | last post by:
Hi guys, Consider the following piece of code: func() { if(x==0) { int arr; /* do something */
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
6
by: ronrsr | last post by:
but I keep getting syntax errors on this one - adict = {'zid': result, 'keywords': result{0], 'quotations':result,'citations':result{3]}; zhtml.print_update_form(adict); result = 22L
6
by: Chris Carlen | last post by:
Hi: I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP. A module (.c file) contains external (to the funcs in that module) variables which are used to affect the operation...
8
by: Latina | last post by:
Hi, I am doing a program of overloaded methods. I want to get what is the union of the user set and S set and the user set and S1 set. I try it but it is not working. Can some one help me please?...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.