printf( )

Discussion in 'C Programming' started by sarma, Jul 12, 2006.

  1. sarma

    sarma Guest

    int a=10;
    printf("%d")


    the output is 10



    int a=10,b=20;
    printf("%d ")


    the output is 20


    how did it return the last defined variable?
    why does printf( ) works from right to left?
    sarma, Jul 12, 2006
    #1
    1. Advertising

  2. sarma

    Chris Dollin Guest

    sarma wrote:

    > int a=10;
    > printf("%d")


    Undefined behaviour (expected argument value missing).
    [Some compilers - eg gcc - can spot this.]

    > the output is 10


    Sheer accident of implementation. (The assigned value
    has probably leaked into the argument register/location
    which printf looks at for the first %-inserted value.)

    > int a=10,b=20;
    > printf("%d ")
    >
    > the output is 20


    Sheer accident with same possible explanation.

    > how did it return the last defined variable?


    Coincidence.

    > why does printf( ) works from right to left?


    Why is the moon made of green fleas?

    --
    Chris "the mice ate the cheese" Dollin
    "Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
    Chris Dollin, Jul 12, 2006
    #2
    1. Advertising

  3. sarma

    Default User Guest

    sarma wrote:

    > int a=10;
    > printf("%d")
    >
    >
    > the output is 10



    This is undefined behavior. Trying to define the behavior of undefined
    behavior is pointless. There's nothing for you to figure out.

    Learn the language properly, from the beginning. Random questions about
    deliberately broken code will tell you little. It wastes your time and
    ours.





    Brian
    Default User, Jul 12, 2006
    #3
  4. sarma

    sarma Guest

    int a,b,c;
    printf ("%u %u %u \n",&a,&b,&c);


    can you predict the result. Assume thet the first variable would be
    stored at an address 65540

    please don't overlook this as a coincidence or an accident

    May be you could find an answer my previous question
    sarma, Jul 13, 2006
    #4
  5. sarma

    Richard Bos Guest

    "sarma" <> wrote:

    > int a,b,c;
    > printf ("%u %u %u \n",&a,&b,&c);
    >
    >
    > can you predict the result.


    No. Nobody can.

    > Assume thet the first variable would be stored at an address 65540


    That is an amazing assumption.

    > please don't overlook this as a coincidence or an accident


    That sentence does does not seem to have a meaningful connection to the
    rest of your post.

    Richard
    Richard Bos, Jul 13, 2006
    #5
  6. sarma

    Guest

    sarma wrote:
    > int a,b,c;
    > printf ("%u %u %u \n",&a,&b,&c);
    >
    >
    > can you predict the result.


    No-one can. The behaviour's undefined since you're passing pointers to
    int and telling printf() it's getting unsigned integers. Even if you
    fix that, it can't be predicted in general. Someone who knows intimate
    details of the compiler you are using, in the mode you are using it, on
    the OS it is running under, on the processor it is running on may be
    able to; but that's not topical here.

    > please don't overlook this as a coincidence or an accident


    What do you mean?

    > May be you could find an answer my previous question


    What do you mean? You got two full, complete, and correct answers to
    your previous question.
    , Jul 13, 2006
    #6
  7. sarma

    Al Balmer Guest

    On 12 Jul 2006 23:42:33 -0700, "sarma" <> wrote:

    >
    >int a,b,c;
    >printf ("%u %u %u \n",&a,&b,&c);
    >
    >
    >can you predict the result. Assume thet the first variable would be
    >stored at an address 65540
    >
    >please don't overlook this as a coincidence or an accident


    What, exactly, are you doing? It looks like you're writing random code
    just to see what happens. If that's the case, please stop wasting our
    time.
    >
    >May be you could find an answer my previous question


    Your previous question was already answered.

    --
    Al Balmer
    Sun City, AZ
    Al Balmer, Jul 13, 2006
    #7
  8. sarma

    John Bode Guest

    sarma wrote:
    > int a,b,c;
    > printf ("%u %u %u \n",&a,&b,&c);
    >
    >
    > can you predict the result.


    The behavior is undefined, therefore the result is unpredictable.

    Had you written

    printf("%p %p %p\n", (void*) &a, (void*) &b, (void*) &c);

    then the behavior would no longer be undefined, but you would need to
    specify exactly which compiler and architecture were being targeted,
    since the result will vary between platforms.

    > Assume thet the first variable would be
    > stored at an address 65540
    >
    > please don't overlook this as a coincidence or an accident
    >
    > May be you could find an answer my previous question


    Your previous question was answered. The answer was that you invoked
    undefined behavior. Unless you're interested in mapping out exactly
    what actions a specific compiler takes as a result of undefined
    behavior (a fool's errand IMO since that can vary based on any number
    of conditions, but whatever), that's as much of an answer as you need.
    John Bode, Jul 13, 2006
    #8
  9. "John Bode" <> writes:
    > sarma wrote:
    >> int a,b,c;
    >> printf ("%u %u %u \n",&a,&b,&c);
    >>
    >>
    >> can you predict the result.

    >
    > The behavior is undefined, therefore the result is unpredictable.
    >
    > Had you written
    >
    > printf("%p %p %p\n", (void*) &a, (void*) &b, (void*) &c);
    >
    > then the behavior would no longer be undefined, but you would need to
    > specify exactly which compiler and architecture were being targeted,
    > since the result will vary between platforms.


    Knowing the compiler would only tell you how the pointer value is
    converted to a sequence of characters; it won't tell you what the
    specific values are. For example, the objects a, b, and c could be
    allocated in any order, and may or may not be adjacent; the
    implementation is under no obligation to document this.

    --
    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, Jul 13, 2006
    #9
    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. Alex Polite

    java printf

    Alex Polite, Jun 5, 2004, in forum: Java
    Replies:
    18
    Views:
    1,200
    Dale King
    Apr 15, 2006
  2. ben
    Replies:
    4
    Views:
    600
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,225
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    424
  5. guru
    Replies:
    8
    Views:
    265
Loading...

Share This Page