Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int i =43; printf("%d",(printf("%d",(printf("%d",i)))));

What I have tried:

My output is 434343. Can any one help me please
Posted
Updated 23-Apr-17 5:19am
Comments
[no name] 23-Apr-17 11:16am    
Help you with what?
Patrice T 23-Apr-17 11:19am    
What is the question ?
No telegraphic style.

No, your output is 4321:
printf("%d",(printf("%d",(printf("%d",i)))));
                          ^             ^
                            Prints "43" and returns the number of characters printed: 2
             ^                            ^
                Prints "2" and returns the number of characters printed: 1
^                                           ^
   Prints "1" and returns the number of characters printed: 1

It's the equivalent of saying:
i = printf("%d",i);
i = printf("%d",i);
printf("%d",i);
As the "innermost" function call has to be executed first.
 
Share this answer
 
Take a look here printf - C++ Reference[^]
int i =43; 
printf("%d",(printf("%d",(printf("%d",i)))));
the bold printf is done first and prints "43", the return value of the printf is 2 (as in 2 characters printed).
int i =43; 
printf("%d",(printf("%d",(printf("%d",i)>)))); 
this bold printf is next and the operand is 2 (the return value from the first call), and so the number "2" gets printed, the return value from this printf is 1 as only one character was printed.
int i =43; 
printf("%d",(printf("%d",(printf("%d",i)>))));
the bold printf here is done last with the number 1 as the operand, so "1" gets printed.

So the expected output would be "4321".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900