Nested printf statement in C with examples

Nested printf in C with an example :

In this tutorial, we will learn what is a nested printf in C program and how to use nested printf in C. Nested printf means one printf another printf.Let’s take a look at the example below to understand the program :

C program :

#include

int main(){
	printf("-%d",printf("-%d-",printf("-%d-",printf("Hello"))));
}

Output :

Hello-5--3--3

Explanation :

– In this nested printf statement, first, the inner printf will execute. It will look like :

output : Hello-
remaining code : printf("-%d",printf("-%d-",printf("-%d-","Hello")));

– Next, the second printf will execute. (the outer one).Since the output of the first printf was Hello-, it will print -5- as 5 is the total character of ‘Hello-‘.It will be like :

output - Hello-5-
printf("-%d",printf("-%d-","-5-");

Next step :

output : Hello-5--3-
remaining code : printf("-%d","-5-");

Final step :

output : Hello-5--3--3

So, the inner printf() will execute first and the control will move to the outer printf() values one by one.

Leave a Reply