Predict the output of the following C program with a printf inside printf.
1. Firstly, the innermost printf is executed which results in printing 1987
2. This printf returns total number of digits in 1987 i.e 4. printf() returns number of characters successfully printed on screen. The whole statement reduces to :
3. The second printf then prints 4 and returns the total number of digits in 4 i.e 1 (4 is single digit number ).
4. Finally, the whole statement simply reduces to :
5. It simply prints 1 and output will be :
So, when multiple printf’s appear inside another printf, the inner printf prints its output and returns length of the string printed on the screen to the outer printf.
This article is contributed by Rishav Raj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to print % using printf()?
- Return values of printf() and scanf() in C/C++
- What is use of %n in printf() ?
- How to change the output of printf() in main() ?
- Passing NULL to printf in C
- Cin-Cout vs Scanf-Printf
- Execution of printf with ++ operators
- What is the difference between printf, sprintf and fprintf?
- Use of & in scanf() but not in printf()
- puts() vs printf() for printing a string
- A nested loop puzzle
- Nested functions in C
- Nested switch case
- C++ | Nested Ternary Operator
- Nested Loops in C with Examples
- How to create a dynamic 2D array inside a class in C++ ?
- Inline namespaces and usage of the "using" directive inside namespaces