output unexpected

Discussion in 'C Programming' started by Vaibhav87@gmail.com, Sep 14, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. James McGill Guest

    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.
     
    James McGill, Sep 14, 2006
    #2
    1. Advertising

  3. <> ha scritto nel messaggio
    news:...
    > 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
     
    Giorgio Silvestri, Sep 14, 2006
    #3
  4. "Giorgio Silvestri" <> ha scritto nel messaggio
    news:HJgOg.107896$...
    >
    > <> ha scritto nel messaggio
    > news:...
    > > 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
     
    Giorgio Silvestri, Sep 14, 2006
    #4
  5. Bill Pursell Guest

    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.
     
    Bill Pursell, Sep 14, 2006
    #5
  6. Default User Guest

    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
     
    Default User, Sep 14, 2006
    #6
  7. 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)
     
    Richard Heathfield, Sep 14, 2006
    #7
  8. "" <> 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) <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.
     
    Keith Thompson, Sep 14, 2006
    #8
  9. 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.
     
    Martin Ambuhl, Sep 14, 2006
    #9
  10. Guest

    but why only 842
    Bill Pursell wrote:
    > 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, 2006
    #10
  11. In article <>,
    <> top 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
     
    Walter Roberson, Sep 15, 2006
    #11
  12. "" <> writes:
    > Bill Pursell wrote:
    >> 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) <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.
     
    Keith Thompson, Sep 15, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kyle Kolander

    unexpected stream output with commas

    Kyle Kolander, May 26, 2005, in forum: C++
    Replies:
    10
    Views:
    548
    Kyle Kolander
    May 27, 2005
  2. Evan Carmi

    Unexpected output while walking dirs

    Evan Carmi, Dec 28, 2006, in forum: Python
    Replies:
    2
    Views:
    312
    Evan Carmi
    Jan 2, 2007
  3. Tom Lam lemontea

    Unexpected repeating of my output function

    Tom Lam lemontea, Oct 22, 2004, in forum: C Programming
    Replies:
    5
    Views:
    313
  4. Sanchit
    Replies:
    8
    Views:
    354
    Keith Thompson
    Mar 22, 2008
  5. 500milesaway
    Replies:
    3
    Views:
    1,848
Loading...

Share This Page