how the following printf statement works

A

aditya

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.
 
R

Ravi Uday

aditya said:
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
 
J

Joona I Palaste

aditya said:
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".
 
S

Stuart Gerchick

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.
 
D

Dan Pop

In said:
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
 
M

Martin Ambuhl

aditya said:
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;
}
 
J

Joona I Palaste

Stuart Gerchick said:
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.
 
D

Dan Pop

In said:
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
 
C

CBFalconer

aditya said:
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.
 
J

Joona I Palaste

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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top