Home »
C solved programs »
C basic programs
printf() statement within another printf() statement in C
Here, we are going to learn how to use and evaluate printf() statement within another printf() statement in C programming language?
Submitted by IncludeHelp, on September 13, 2018
A printf() function is a standard library function, that is used to print the text and value on the standard output screen. Here, we will evaluate the expression – where a printf() is used within another printf() statement.
Consider the statement:
printf ("%d", printf ("Hello"));
Note these points:
- printf() prints the text as well as the value of the variable, constant etc.
- printf() returns an integer value that is the total number of printed characters including spaces, carriage return, line feed etc.
Evaluation of the above written statement
printf() function evaluates from right to left, thus printf("Hello") will be evaluated first, that will print "Hello" and printf("Hello") will return the total number of printed character that is 5 and then the output of this printf("Hello") after printing "Hello" will be 5.
Thus, finally, the output of the above-written statement will be: "Hello5".
Example
#include <stdio.h>
int main(void)
{
printf("%d", printf ("Hello"));
return 0;
}
Output
Hello5
Using more than one printf() within printf() statement
Consider the following statement:
printf ("%d%d", printf ("Hello") , printf ("friends"));
Evaluation
As we said above that the printf() arguments evaluates from right to left, thus, printf("Friends") will be evaluated first and return 7, after that statement printf("Hello") will be evaluated and return 5. Thus, the final output will be "friendsHello57".
Example
#include <stdio.h>
int main(void)
{
printf ("%d%d", printf ("Hello"), printf ("Friends"));
return 0;
}
Output
FriendsHello57
C Basic Programs »