简体   繁体   中英

Return value of printf() function in C

The printf() function will return the number of characters printed. But in the code below why is it printing 5.

int a=1000;
printf("%d",printf("\n%d",a));

It prints "1000" once and a space, so altogether we have 2 characters.

It should output "1000 2". But its outputting "1000 5".

The number of characters output is 5. 1000 is four characters. \n is one character.

printf doesn't return the number of "items" output like the scanf family of functions do for input. It returns the actual character count.

The inner call happens first, prints 5 characters ( \n , 1 , 0 , 0 , 0 ) and returns 5 .

The outer call then happens, and prints the 5 that was returned by the inner call.

suppose expression:

int a=10;
printf("a=%d",printf("b=%d",a));

output

b=10 a=4;

b because of value of assigned to b ie

b=10;

and

b,=,1,0   

count as four and assigns to the a ie:

a=4;

Lets first check the output of inner printf:

/n, 1, 0, 0, 0

Now you need to consider 2 things:

1) You have to take escape sequences like '\n','\t' etc into account.
2) You have to take 1 escape sequence as 1 character (not 2)

The outer printf returns the actual character count of inner printf which is 5. So outer printf returns 5.

printf() returns the actual character count where as here we have 4 ("1000") + 1 ("\n") character so it will give the output 1000 and then 5 which is the character count of inner printf function and it looks like 10005

You should clearly notice that 1000 is 4 letters and you have \n which it is a character by it self

The number 1000 is composed of four digits therefore it take four characters to print it. Four character plus the line feed is five characters.

in printf("%d",printf("\n%d",a)); the printf("\n%d",a) will print a newline char '\n' and the integer value 1000 which makes a total of 5 characters. The first inner printf is called first which prints the newline and 1000, and then the returned value 5 is printed by the outer printf .

Printf return the number of character successfully printed by the function.

printf() returns total no. of character printed on console, you pass 1000; so first inner printf() function will work and print 1000,and here no. of character is 4. One is \n.

So total no. of character become 5, that's why it print 1000 5.

在此处输入图像描述

As you can see in the image, first the last printf shows what's it's supposed to show ie 3223433 1233. Now, the last printf returns the number of characters in the string/int which it displayed. Then, the second last printf displays 12 which is the length of the whatever is displayed by last printf ie 3223433 1233. The third last printf now displays 2 which is the length of 12. Since, length of 12 is 2, it is displayed next and then since length of 2 is 1, 1 is displayed and 1 again at last, since it's the length of 1.

Just to add a bit more, the number of character returned by printf() may depends on the specifiers of the parameters

ex:

int a= 0xff;
printf(" : %i characters\n" ,printf("%x",a)); //prints ff : 2 characters
printf(" : %i characters\n" ,printf("%d",a)); //prints 255 : 3 characters
int a=1000;
printf("%d",printf("\n%d",a));

There are 2 printf() functions in this code fragment:

  1. inner printf()
  2. outer printf()

Firstly, the inner printf() will execute and will print 1000 and then the outer printf() will execute.

It is to be noted that the printf() returns the number of characters that it prints and the escape sequences are counted as a character in printf().

Thus, by executing the inner printf(), we have got 5(because '\n', 1, 0,0,0 are 5 characters), Now, when the outer printf() is executed, 5 is printed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM