Q:

printf() statement within another printf() statement in C

0

printf() statement within another printf() statement in C

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:

  1. printf() prints the text as well as the value of the variable, constant etc.
  2. 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".

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
printf() examples/variations in C... >>
<< Printing an address of a variable in C...