Knowledge Base
Home C

printf() Function Return Value

Category: C | Comments (5)

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.


Next: Importance of Header Files


Comments

the output is not 10 10 10 5
but i m getting 10 10 108
Comment posted by: chaitanya1206 on 2008-01-16T00:08:35
probably it is taking the value 10 as two characters.then the output ll be 10 10 10 8
Comment posted by: chaitanya1206 on 2008-01-16T00:10:40
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
Comment posted by: shruti k on 2008-02-03T03:50:07
when a was taken as 3 he output is correct,ie.,5. So, you guessed right....
Comment posted by: payel on 2008-02-08T11:51:08
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.
Comment posted by: ShalabhSoni on 2008-05-27T17:27:17


Post Comment


Members Please Login

Name:


Email:
 
(Optional. Used for Notification)

Title:

 
Comment:


Validation Code:
 <=>  (Enter this code in text box)
Subscribe





Google Sponsored Links

 

Daily Email Updates

Get Latest Knowledge Base Updates delivered directly to your Inbox...

Enter your email address:

Latest Knowledge Base Updates
 

C Tutorials

 

Related Tutorials