Please explain the output

Discussion in 'C Programming' started by bhawna, Mar 27, 2011.

  1. bhawna

    bhawna Guest

    #include<stdio.h>
    int main()
    {

    int i=99,a=3,p=44;
    printf("%d %d %d");
    return 0;
    }

    /*Output*/
    134518324 99 3

    Is the output compiler dependent?
    bhawna, Mar 27, 2011
    #1
    1. Advertising

  2. bhawna

    Ian Collins Guest

    On 03/27/11 02:25 PM, bhawna wrote:
    > #include<stdio.h>
    > int main()
    > {
    >
    > int i=99,a=3,p=44;
    > printf("%d %d %d");
    > return 0;
    > }
    >
    > /*Output*/
    > 134518324 99 3
    >
    > Is the output compiler dependent?


    The output is undefined.

    --
    Ian Collins
    Ian Collins, Mar 27, 2011
    #2
    1. Advertising

  3. bhawna <> writes:

    > #include<stdio.h>
    > int main()
    > {
    >
    > int i=99,a=3,p=44;
    > printf("%d %d %d");
    > return 0;
    > }
    >
    > /*Output*/
    > 134518324 99 3
    >
    > Is the output compiler dependent?


    Not even that. I get different output every time I run the program.
    The problem is that the behaviour is not defined by the specification of
    the C programing language so there is no reasonable way to ascribe any
    meaning to the code. There is not even any obligation on the compiler
    to generate an executable from this source.

    --
    Ben.
    Ben Bacarisse, Mar 27, 2011
    #3
  4. On 2011-03-27, bhawna <> wrote:
    > #include<stdio.h>
    > int main()
    > {
    >
    > int i=99,a=3,p=44;
    > printf("%d %d %d");
    > return 0;
    > }
    >
    > /*Output*/
    > 134518324 99 3
    >
    > Is the output compiler dependent?


    Yes, and system, and possibly even invocation dependent if the
    address space is randomised each invocation to prevent shellcode
    attacks. The implementation is free to segfault or anything else
    it likes in this situation, but the most likely real-world outcome
    is as here, where it seems that printf is looking on the stack for
    the missing parameters. Since none have been placed there it is
    finding things that are not really intended for it, at least not
    in this context. It finding the storage allocated to i and a should
    therefore come as no surprise. I imagine the 134518324 is the return
    address for when printf returns - if you look it up in a debugger
    you will probably find it is the address of the return statement.

    --
    Andrew Smallshaw
    Andrew Smallshaw, Mar 27, 2011
    #4
  5. bhawna

    bhawna Guest

    Thanks
    bhawna, Mar 27, 2011
    #5
  6. Joe Wright <> writes:
    > On 3/26/2011 21:25, bhawna wrote:
    >> #include<stdio.h>
    >> int main()
    >> {
    >>
    >> int i=99,a=3,p=44;
    >> printf("%d %d %d");
    >> return 0;
    >> }
    >>
    >> /*Output*/
    >> 134518324 99 3
    >>
    >> Is the output compiler dependent?

    > Try..
    >
    > #include<stdio.h>
    > int main(void) {
    > int i=99,a=3,p=44;
    > printf("%d %d %d", i, a, p);
    > return 0;
    > }
    > output is..
    > 99 3 44
    > ..as you might expect.


    bhawna, this is why it's helpful to be more explicit in your
    question, particularly about why you're asking. You asked whether
    the output is compiler dependent, but you didn't say why you thought
    it might be. You could plausibly have just forgotten to pass i,
    a, and p as arguments. You could equally plausibly have done so
    deliberately, and been curious about the consequences.

    Oh, and you really should have a "\n" at the end of the output;
    some systems may not show the output correctly without it.

    (And to nitpick even further, "int main()" should be
    "int main(void)".)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 27, 2011
    #6
  7. bhawna

    zooZodoy Guest

    Am 27.03.2011 03:25, schrieb bhawna:
    > #include<stdio.h>
    > int main()
    > {
    >
    > int i=99,a=3,p=44;
    > printf("%d %d %d");
    > return 0;
    > }
    >
    > /*Output*/
    > 134518324 99 3
    >
    > Is the output compiler dependent?



    an int is 4 byte
    an adress is byte

    have a look at the Stack:


    StackPointer Value (Value in hex)
    zooZodoy, Mar 27, 2011
    #7
  8. Joe Wright <> writes:
    > On 3/27/2011 04:02, Keith Thompson wrote:
    >> Joe Wright<> writes:

    [...]
    >> bhawna, this is why it's helpful to be more explicit in your
    >> question, particularly about why you're asking. You asked whether
    >> the output is compiler dependent, but you didn't say why you thought
    >> it might be. You could plausibly have just forgotten to pass i,
    >> a, and p as arguments. You could equally plausibly have done so
    >> deliberately, and been curious about the consequences.
    >>
    >> Oh, and you really should have a "\n" at the end of the output;
    >> some systems may not show the output correctly without it.
    >>
    >> (And to nitpick even further, "int main()" should be
    >> "int main(void)".)
    >>

    > Hi Keith. I should have added "\n" in the format string. I did change "int
    > main()" to "int main(void)". You missed it. If I want authority on C here I
    > choose you, now that Heathfield and Pop have quit. I really miss Dan
    > Pop.


    I comments were mostly directed at the OP, not at you. My main point
    was really that the OP didn't provide enough information for you (or
    anyone else) to understand just what he was asking, except in the most
    literal sense.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 27, 2011
    #8
  9. bhawna

    Ian Collins Guest

    On 03/28/11 05:56 AM, zooZodoy wrote:
    > Am 27.03.2011 03:25, schrieb bhawna:
    >> #include<stdio.h>
    >> int main()
    >> {
    >>
    >> int i=99,a=3,p=44;
    >> printf("%d %d %d");
    >> return 0;
    >> }
    >>
    >> /*Output*/
    >> 134518324 99 3
    >>
    >> Is the output compiler dependent?

    >
    >
    > an int is 4 byte
    > an adress is byte


    Says who?

    --
    Ian Collins
    Ian Collins, Mar 27, 2011
    #9
  10. bhawna

    Hans Vlems Guest

    On 27 mrt, 03:25, bhawna <> wrote:
    > #include<stdio.h>
    > int main()
    > {
    >
    >         int i=99,a=3,p=44;
    >         printf("%d %d %d");
    >         return 0;
    >         }
    >
    > /*Output*/
    > 134518324 99 3
    >
    > Is the output compiler dependent?


    It sure helps if you tell the compiler what variables you want to
    print with printf.
    printf takes two kinds of input. The first is a format-string that
    tells printf how to organize the lay-out of the output.
    Count the number of %-signs and that tells you how many variables must
    follow the format string.
    In your example there are three %-signs, so printf expects three
    variables while you gave it none at all.
    Printf doesn't complain however, it just prints data that sits in
    memory locations that weren't specified so it prints garbage.
    Hans
    Hans Vlems, Mar 28, 2011
    #10
    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. srikanth
    Replies:
    14
    Views:
    530
    vijay
    Jul 10, 2003
  2. KK
    Replies:
    2
    Views:
    500
    Big Brian
    Oct 14, 2003
  3. Jaspreet

    Please explain the output

    Jaspreet, Sep 16, 2005, in forum: C Programming
    Replies:
    22
    Views:
    660
    Keith Thompson
    Sep 20, 2005
  4. Aarti
    Replies:
    3
    Views:
    259
  5. Kaye Ng
    Replies:
    8
    Views:
    162
    Josh Cheek
    Jun 8, 2010
Loading...

Share This Page