printf() statement within another printf() statement in C
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:
Output
Using more than one printf() within printf() statement
Consider the following statement:
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:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer