GeekInterview.com
  I am new, Sign me up!
 
Home C
 

printf() Function Return Value

 
Category: C
Comments (8)


What is the return value from printf() function?


printf function always returns the number of characters printed. Let us understand this with an example:


main()
{
int a=10;
printf("%d",printf("%d %d %d", a,a,a));
}


In this above program the inner printf is first called which prints value of a, three times with space between each value, 10 10 10. Total 5 characters get printed (3 value of 10 and 2 spaces).


As explained earlier the inner printf after printing the values, returns the number of characters printed, 5 which is printed by the outer printf.


The output of the above program is


10 10 10 5


A function always returns a value and printf function returns the number of characters successfully printed.



Read Next: Importance of Header Files



 

 

Comments


chaitanya1206 said:

  the output is not 10 10 10 5
but i m getting 10 10 108
January 16, 2008, 12:08 am

chaitanya1206 said:

  probably it is taking the value 10 as two characters.then the output ll be 10 10 10 8
January 16, 2008, 12:10 am

shruti k said:

  i used "t" with every %d in the inner printf,
n i got the output.....
10 10 10 9
dis may b bcoz of the spaces as if we take 1 n 0 as different characters,n count the spaces as well,the total comes out to b 9
1.......1
0.......2
.......3
1.......4
0.......5
.......6
1.......7
0.......8
.......9
February 3, 2008, 3:50 am

payel said:

  when a was taken as 3 he output is correct,ie.,5. So, you guessed right....
February 8, 2008, 11:51 am

ShalabhSoni said:

  The inner printf will return 8, as it has printed 8 character thrice has it printed a (2*8), as a is two characters wide.and it has printed two spaces as well(between %d %d %d) so total is 8 characters.So the answer comes to be 10 10 108. as first the inner printf executes than the outer one.
May 27, 2008, 5:27 pm

udhayakumar said:

  printf() returns that how many values it was read.eg:printf() also return the value that let see with eg.
#include
#include
void main()
{
int l,a=10,b=20,c=30;
clrscr();
l=printf("%d%d",a,b);
printf("first printf()=%dn",l);
l=printf("%d%d%d",a,b,c);
printf("second printf()=%dn",l);
}
output will be:
first printf()=2
second printf()=3
explanation:
in first printf() it reads two values so that output shown 2,as like second printf() shown output value 3

January 2, 2009, 6:36 am

krishna said:

  printf is used to get the values which are we insert in the prog or runtime the syntax of the printf
printf("",);
April 24, 2009, 8:10 am

manish bihari arora said:

  It counts how many unit is printed like
if you are going to print 10 10 10
then printf count as 12345678=total 8 and its true....
June 16, 2009, 3:02 pm

niraj.mailbox said:

  i m getting the output
1010106
how
October 19, 2009, 6:36 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape