469,642 Members | 1,103 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,642 developers. It's quick & easy.

how the following printf statement works

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.
Nov 14 '05 #1
8 2449

"aditya" <ad************@gmail.com> wrote in message
news:2c*************************@posting.google.co m...
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
Nov 14 '05 #2
aditya <ad************@gmail.com> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"This is a personnel commuter."
- Train driver in Scientific American
Nov 14 '05 #3
In <2c*************************@posting.google.com> ad************@gmail.com (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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #4
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;
}
Nov 14 '05 #5
Stuart Gerchick <sg*******@bloomberg.net> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #6
In <cl**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Stuart Gerchick <sg*******@bloomberg.net> 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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #7
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8
Dan Pop <Da*****@cern.ch> scribbled the following:
In <cl**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Stuart Gerchick <sg*******@bloomberg.net> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Adi | last post: by
7 posts views Thread by ssantamariagarcia | last post: by
7 posts views Thread by Martin Jørgensen | last post: by
4 posts views Thread by pai | last post: by
51 posts views Thread by Spidey | last post: by
16 posts views Thread by Ravi | last post: by
10 posts views Thread by saipathakota | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.