Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > output unexpected

Reply
Thread Tools

output unexpected

 
 
Vaibhav87@gmail.com
Guest
Posts: n/a
 
      09-14-2006
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?

 
Reply With Quote
 
 
 
 
James McGill
Guest
Posts: n/a
 
      09-14-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Giorgio Silvestri
Guest
Posts: n/a
 
      09-14-2006

<(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) 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



 
Reply With Quote
 
Giorgio Silvestri
Guest
Posts: n/a
 
      09-14-2006

"Giorgio Silvestri" <(E-Mail Removed)> ha scritto nel messaggio
news:HJgOg.107896$(E-Mail Removed).. .
>
> <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed) 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




 
Reply With Quote
 
Bill Pursell
Guest
Posts: n/a
 
      09-14-2006
(E-Mail Removed) 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.

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-14-2006
(E-Mail Removed) 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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-14-2006
(E-Mail 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?

--
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)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-14-2006
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-14-2006
(E-Mail Removed) 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.





 
Reply With Quote
 
Vaibhav87@gmail.com
Guest
Posts: n/a
 
      09-15-2006
but why only 842
Bill Pursell wrote:
> (E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected output in Post-translate Simulation 500milesaway VHDL 3 03-11-2009 06:59 PM
Unexpected Output when reading a files using read() Sanchit C Programming 8 03-22-2008 02:40 AM
Unexpected output while walking dirs Evan Carmi Python 2 01-02-2007 10:50 AM
unexpected stream output with commas Kyle Kolander C++ 10 05-27-2005 09:58 PM
Unexpected repeating of my output function Tom Lam lemontea C Programming 5 11-13-2004 01:28 PM



Advertisments