output unexpected

V

Vaibhav87

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?
 
B

Bill Pursell

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

Default User

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
 
R

Richard Heathfield

(e-mail address removed) 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?
 
K

Keith Thompson

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

Martin Ambuhl

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

Vaibhav87

but why only 842
Bill said:
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.
 
W

Walter Roberson

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:

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

Keith Thompson

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top