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:

  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".

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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.