473,509 Members | 3,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

output unexpected

i write
void main()
{
int j;
j - = 0;
printf("%d",y);
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?

Sep 14 '06 #1
11 1842

<Va*******@gmail.comha scritto nel messaggio
news:11**********************@p79g2000cwp.googlegr oups.com...
i write
void main()
int main(void)
{
int j;
j - = 0;
printf("%d",y);
y ?

printf("%d\n",j);

j must be initialized!
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?


--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer

Sep 14 '06 #2

"Giorgio Silvestri" <gi**************@libero.itha scritto nel messaggio
news:HJ**********************@twister1.libero.it.. .
>
<Va*******@gmail.comha scritto nel messaggio
news:11**********************@p79g2000cwp.googlegr oups.com...
i write
void main()

int main(void)
{
int j;
j - = 0;
printf("%d",y);

y ?

printf("%d\n",j);

j must be initialized!
and

return 0;
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?


--
Giorgio Silvestri
DSP/Embedded/Real Time OS Software Engineer


Sep 14 '06 #3
Va*******@gmail.com wrote:
i write
void main()
{
int j;
j's value is undefined.
j - = 0;
printf("%d",y);
y is entirely undeclared. This should not even compile as written.
Sep 14 '06 #4
Va*******@gmail.com wrote:
i write
void main()
{
int j;
j - = 0;
printf("%d",y);
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?
You must not be posting the code you are running, as
the code snippet above doesn't compile. (y is
undeclared, and the line "j - = 0" is a syntax error.)

Assuming you meant:
#include <stdio.h>

int main(void)
{
int j;
j -= 0;
printf("%d",j);
return 0;
}

The answer is that j starts out with an indeterminate
value which you then decrement by zero and print.
The value of j could be anything.

Sep 14 '06 #5
Va*******@gmail.com wrote:
i write
void main()
{
int j;
j - = 0;
printf("%d",y);
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?

The answer is that same as the last time you posted. Don't waste our
time.


Brian
Sep 14 '06 #6
Va*******@gmail.com said:
i write
void main()
{
int j;
j - = 0;
printf("%d",y);
}
& i got the answer as 842.
I got several diagnostic messages from my compiler, which refused to produce
an executable program:

gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
-Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
foo.c:2: warning: function declaration isn't a prototype
foo.c:2: warning: return type of `main' is not `int'
foo.c: In function `main':
foo.c:4: parse error before `='
foo.c:5: warning: implicit declaration of function `printf'
foo.c:5: `y' undeclared (first use in this function)
foo.c:5: (Each undeclared identifier is reported only once
foo.c:5: for each function it appears in.)
make: *** [foo.o] Error 1
i tried it on various computers but the
answer is same.
I'm surprised you got any compiler to produce an executable program.
pls help me
how is this answer is?
What answer were you expecting it to produce, and why?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 14 '06 #7
"Va*******@gmail.com" <Va*******@gmail.comwrites:
i write
void main()
{
int j;
j - = 0;
printf("%d",y);
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?
No, you didn't.

That code doesn't compile. I explained why in great detail the last
time you posted it.

Fix the errors and post the *exact* code that you're having trouble
with. Stop wasting our time by posting code that won't even compile.

--
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.
Sep 14 '06 #8
Va*******@gmail.com wrote:
i write
void main()
{
int j;
j - = 0;
printf("%d",y);
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?
1) You failed to #include <stdio.h>, which contains a prototype for
printf. Because printf takes a variable number of arguments, failure to
provide a prototype is a very bad thing.

2) main returns an int. Your definition of main with a void return type
is a very bad thing.

3) you have not initialized j. Failure to initialize variables is a
very bad thing.

4) "-=" is one token. "- =" is two tokens. Confusing them is a very bad
thing.

5) You never declare the variable y. Not declaring variables before use
is a very bad thing.

6) You return no value from main(). In C89 this is a very bad thing; in
C99 it is only bad practice.

7) No one could in good faith write such an error-laden "program" if he
had ever opened a book on C. This suggests you purposely posted crap.
That is a very bad thing.

Sep 14 '06 #9
but why only 842
Bill Pursell wrote:
Va*******@gmail.com wrote:
i write
void main()
{
int j;
j - = 0;
printf("%d",y);
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?

You must not be posting the code you are running, as
the code snippet above doesn't compile. (y is
undeclared, and the line "j - = 0" is a syntax error.)

Assuming you meant:
#include <stdio.h>

int main(void)
{
int j;
j -= 0;
printf("%d",j);
return 0;
}

The answer is that j starts out with an indeterminate
value which you then decrement by zero and print.
The value of j could be anything.
Sep 15 '06 #10
In article <11**********************@d34g2000cwd.googlegroups .com>,
Va*******@gmail.com <Va*******@gmail.comtop posted:
>but why only 842
With respect to the question of why, on "various computers", some
code with undefined behaviour always happened to produce the same result:
>Bill Pursell wrote:
>#include <stdio.h>
>int main(void)
{
int j;
j -= 0;
printf("%d",j);
return 0;
}
The answer, Vaibhav87, is that you did not happen to use a variety
of computers or compilers. All the machines you happened to try
were the same as far as the compilers were concerned.

On SGI IRIX with SGI's MipsPro C compiler, the code happens to
produce 0.

On the same SGI IRIX machine with gcc 3.3, the code
happens to produce 2147430164 which is more easily recognized in
its hex format, 0x7fff2f14 . That happens to be a typical stack pointer
address in this machine.

So what has happened is that on stack-based compilers, the compiler
reaches in to the stack to the offset that it expects the variable to
be at, and uses whatever value it happens to find there, but it
does not specifically initialize the value before it starts because
it hasn't been told to do that.

On the machines you've been trying, the value that happens to be lying
on the stack has been 842.
I will make a prediction: whatever machine you are trying it on has
16 bit int. 842 decimal is 0x034a hex, which could easily be
the bottom 16 bits of a 32 bit pointer that just happened to be in
memory there as a side-effect of the program start-up. Or it could
have originated with something else completely. If you *really*
want to know why your *particular* machines happen to produce 842,
disassemble the executable and trace through it step by step keeping
track of everything that gets written to memory; somewhere along the
way you'll find that as a side effect of something else, 0x034a or
0x4a03 happens to get written to the location that j later occupies.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Sep 15 '06 #11
"Va*******@gmail.com" <Va*******@gmail.comwrites:
Bill Pursell wrote:
>Va*******@gmail.com wrote:
i write
void main()
{
int j;
j - = 0;
printf("%d",y);
}
& i got the answer as 842. i tried it on various computers but the
answer is same. pls help me
how is this answer is?

You must not be posting the code you are running, as
the code snippet above doesn't compile. (y is
undeclared, and the line "j - = 0" is a syntax error.)

Assuming you meant:
#include <stdio.h>

int main(void)
{
int j;
j -= 0;
printf("%d",j);
return 0;
}

The answer is that j starts out with an indeterminate
value which you then decrement by zero and print.
The value of j could be anything.

but why only 842
Please don't top-post. Read <http://www.caliburn.nl/topposting.html>.

You *still* have not posted your actual code. You've only posted some
seemingly random chunk of text that looks something like a C program,
but isn't.

Apparently you're getting a result of 842 from *something*, but unless
you tell us what you're doing, we can't possibly guess what's going
on.

Show us your actual program. Don't re-type it, copy-and-paste it; we
need to see the *exact* program that you are actually compiling and
running.

You have been told this many times, and yet you continue to waste our
time asking about the behavior of a program that you refuse to show
us.

Show us your program, and we'll discuss it. Until then, *stop wasting
our time*.

--
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.
Sep 15 '06 #12

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

Similar topics

0
1646
by: Steve Holden | last post by:
For quite a while now I've been filling part of the navigation-bar on my home page with Python-related news extracted from O'Reilly's meerkat service. I've been experiencing intermittent...
0
5128
by: rtm | last post by:
I am interested in running a process with a timeout. Also I'm interested in analyzing the output of this process. Under Unix, the solution is described clearly in the Perl Cookbook "16.10: ...
10
1708
by: Kyle Kolander | last post by:
Got a strange bug going on here... // proper includes and additional code here int x = 2552123; cout << x; This results in output like this: 2,552,123 Same behavior with stringstreams. ...
5
2158
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned...
4
8290
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG:...
6
1265
by: Peter Strøiman | last post by:
Hi. I have a situation where I have a web project and a class library in the same solution. The web project uses the class library. Therefore, I created a "project reference" in the web project...
0
1000
by: Pradnyesh Sawant | last post by:
Hello, I need the cpu usage of a linux box, for which i capture the output of "top" using "popen". However, i am facing problems during string handling. The code snippet is:- top =...
15
1800
by: jools | last post by:
I'm having trouble modifying some code written by someone else. The code is very dense and obscure but does work fine. However I need to insert a block of my own and I've hit what I assume is a...
11
2905
by: JRough | last post by:
I'm trying to use output buffering to cheat so i can print to excel which is called later than this header(). header("Content-type: application/xmsdownload"); header("Content-Disposition:...
0
1793
by: JRough | last post by:
On Sep 26, 3:23 pm, Captain Paralytic <paul_laut...@yahoo.comwrote: without the exit I get this again: Warning: Cannot modify header information - headers already sent by (output started at...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7347
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7416
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5062
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1571
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
779
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
443
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.