By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

output unexpected

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a

<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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.