how the following printf statement works

Discussion in 'C Programming' started by aditya, Oct 29, 2004.

  1. aditya

    aditya Guest

    hi,

    Can anybody please tell me that how the following printf(...)

    statement works-

    main(){
    int d=9;
    printf("%d",printf("%d"));
    return 0;
    }

    The output is 91.

    Thanks,

    Aditya.
    aditya, Oct 29, 2004
    #1
    1. Advertising

  2. aditya

    Ravi Uday Guest

    "aditya" <> wrote in message
    news:...
    > hi,
    >
    > Can anybody please tell me that how the following printf(...)
    >
    > statement works-
    >
    > main(){
    > int d=9;
    > printf("%d",printf("%d"));


    I think you missed out argument d, to the sec printf !!

    > return 0;
    > }
    >
    > The output is 91.
    >
    > Thanks,
    >
    > Aditya.


    On my system it gives 91 if properly edited

    Try including the necessary headers and then exec:

    bash-2.02$ cat t12.c

    #include <stdio.h>
    int main()
    {
    int d=9;

    printf("%d",printf("%d", d));
    printf ("\n"); /* Needed for the output to be displayed ! */
    return 0;
    }
    bash-2.02$ gcc -pedantic -Wall -ansi t12.c
    bash-2.02$ ./a.exe
    91

    Initially the inner/second 'printf' prints the value of d == 9, then return
    value of inner printf is taken as a argument
    to the outer printf, which then prints 1.

    From the standard:

    4.9.6.3 The printf function

    Synopsis

    #include <stdio.h>
    int printf(const char *format, ...);

    Description

    The printf function is equivalent to fprintf with the argument
    stdout interposed before the arguments to printf .

    Returns

    --> *The printf function returns the "number of characters transmitted",
    or a negative value if an output error occurred.*

    So in your case the number of characters transmitted by the sec. printf is
    1 (which is '9')
    hence you can see the output as "91"

    - Ravi
    Ravi Uday, Oct 29, 2004
    #2
    1. Advertising

  3. aditya <> scribbled the following:
    > hi,


    > Can anybody please tell me that how the following printf(...)


    > statement works-
    >
    > main(){
    > int d=9;
    > printf("%d",printf("%d"));
    > return 0;
    > }


    > The output is 91.


    This code causes undefined behaviour, so the output might be anything it
    wants, including "Your mother was a hamster and your father smelt of
    elderberries!". The outer printf() function expects to print an integer
    number, and this integer number comes from the return value of the
    inner printf(). printf()'s return value is defined to be the number of
    bytes it actually managed to print. However, the inner printf()
    function also expects to print an integer number, but it is never given
    one. This is what causes undefined behaviour, because printf() is now
    forced to get the integer number from some undefined place. This might
    be the execution stack (if there is one), a random address in memory,
    your pet iguana's mind, or whatever else your implementation fancies.
    So the answer to your question is "No, it doesn't work".

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "This is a personnel commuter."
    - Train driver in Scientific American
    Joona I Palaste, Oct 29, 2004
    #3
  4. printf("%d",printf("%d"));

    remember that printf returns the number of characters printed.
    printf("%d") prints 9 and returns 1....so the first %d prints the
    return value of 1.
    Stuart Gerchick, Oct 29, 2004
    #4
  5. aditya

    Dan Pop Guest

    Re: how the following printf statement works

    In <> (aditya) writes:

    >Can anybody please tell me that how the following printf(...)
    >
    >statement works-
    >
    > main(){
    > int d=9;
    > printf("%d",printf("%d"));
    > return 0;
    >}
    >
    >The output is 91.


    Not on the system I've tried it:

    mentor:~/tmp 12> cat test.c
    main(){
    int d=9;
    printf("%d",printf("%d"));
    return 0;
    }
    mentor:~/tmp 13> cc test.c
    mentor:~/tmp 14> ./a.out
    -133609449mentor:~/tmp 15> gcc test.c
    mentor:~/tmp 16> ./a.out
    665605mentor:~/tmp 17>

    This piece of junk invokes undefined behaviour twice and doesn't even
    generate a properly terminated line of output (hence my shell prompt gets
    appended to your program's output).

    You CANNOT use printf without declaring it first (it's a variadic
    function) and no printf call can supply fewer arguments than expected
    by its format string.

    So, let's turn it first into a program with a well defined output:

    #include <stdio.h>

    int main()
    {
    int d = 9;

    printf("%d\n", printf("%d", d));
    return 0;
    }

    This program outputs 91 by design and not by pure accident. So, the
    output is the same, no matter where you compile and execute it.

    The inner printf gets executed first. It displays the value of d, which
    is 9 and returns the number of displayed characters (1, in this case) to
    its caller. The caller, which is the outer printf call, displays this
    value and terminates the line with a newline character.

    It is possible to explain why the original program displays 91 on the
    vanilla x86 implementation, but you're not going to learn anything
    about the C language from that explanation. It is an instructive
    exercise for people interested in how x86 C compilers work, however,
    but, since it's off topic in this newsgroup, anyway, I won't bother
    writing the explanation. It involves the way automatic variables are
    allocated and function arguments are passed.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
    Dan Pop, Oct 29, 2004
    #5
  6. aditya wrote:

    > hi,
    >
    > Can anybody please tell me that how the following printf(...)
    >
    > statement works-
    >
    > main(){
    > int d=9;
    > printf("%d",printf("%d"));
    > return 0;
    > }
    >
    > The output is 91.

    Only by accident.


    /* mha: Please not the changes in your code. The code you posted does
    *not* do what you claimed. */
    #include <stdio.h> /* mha: A prototype for the variadic
    function printf is REQUIRED even in
    C89 */

    int /* mha: main always returns an int in a
    hosted system. Say so. This is
    REQUIRED in C99 */
    main(void)
    {
    int d = 9;
    #if 0
    /* mha: original printf call. Note that the outer one fails to have
    portable behavior because it has no end-of-line character for the
    last line of output and the inner one has the wrong number of
    arguments. That missing argument is why the code doesn't print
    the '9' you claim except by accident. */
    printf("%d", printf("%d"));
    #endif
    printf("%d\n", printf("%d", d)); /* mha: new printf */
    return 0;
    }
    Martin Ambuhl, Oct 29, 2004
    #6
  7. Stuart Gerchick <> scribbled the following:
    > printf("%d",printf("%d"));


    > remember that printf returns the number of characters printed.
    > printf("%d") prints 9 and returns 1....so the first %d prints the
    > return value of 1.


    printf("%d") does not print 9 and return 1. It's not guaranteed to,
    anyway. It might legally print and return anything it wants.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "Make money fast! Don't feed it!"
    - Anon
    Joona I Palaste, Oct 29, 2004
    #7
  8. aditya

    Dan Pop Guest

    Re: how the following printf statement works

    In <cltqek$nb4$> Joona I Palaste <> writes:

    >Stuart Gerchick <> scribbled the following:
    >> printf("%d",printf("%d"));

    >
    >> remember that printf returns the number of characters printed.
    >> printf("%d") prints 9 and returns 1....so the first %d prints the
    >> return value of 1.

    >
    >printf("%d") does not print 9 and return 1. It's not guaranteed to,
    >anyway. It might legally print and return anything it wants.


    And this is what it actually does. The 9 is an "accident" happening
    under very specific conditions (one stack used for everything, function
    arguments pushed on the stack starting with the last, the function
    return address pushed after the arguments, the return value either not
    allocated on the stack or allocated after all the arguments).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
    Dan Pop, Oct 29, 2004
    #8
  9. aditya

    CBFalconer Guest

    aditya wrote:
    >
    > Can anybody please tell me that how the following printf(...)
    > statement works-
    >
    > main(){
    > int d=9;
    > printf("%d",printf("%d"));
    > return 0;
    > }
    > The output is 91.


    It doesn't. Apart from the illegal invocation of main, and the
    lack of <stdio> inclusion, the printf statements invoke undefined
    behavior. Try:

    printf("%d\n", printf("%d ->", 789));

    and remember that printf returns the number of chars printed, and
    that a functions arguments are evaluated before a function is
    called.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 29, 2004
    #9
  10. Re: how the following printf statement works

    Dan Pop <> scribbled the following:
    > In <cltqek$nb4$> Joona I Palaste <> writes:
    >>Stuart Gerchick <> scribbled the following:
    >>> printf("%d",printf("%d"));

    >>
    >>> remember that printf returns the number of characters printed.
    >>> printf("%d") prints 9 and returns 1....so the first %d prints the
    >>> return value of 1.

    >>
    >>printf("%d") does not print 9 and return 1. It's not guaranteed to,
    >>anyway. It might legally print and return anything it wants.


    > And this is what it actually does. The 9 is an "accident" happening
    > under very specific conditions (one stack used for everything, function
    > arguments pushed on the stack starting with the last, the function
    > return address pushed after the arguments, the return value either not
    > allocated on the stack or allocated after all the arguments).


    Yes, I agree completely. That's why I said "it's not guaranteed to,
    anyway".

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "To err is human. To really louse things up takes a computer."
    - Anon
    Joona I Palaste, Oct 29, 2004
    #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. Replies:
    2
    Views:
    312
    Richard Herring
    Apr 26, 2005
  2. ben
    Replies:
    4
    Views:
    612
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

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

    printf affects following printf/s

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

Share This Page