简体   繁体   English

C 中 printf() function 的返回值

[英]Return value of printf() function in C

The printf() function will return the number of characters printed. printf() function 将返回打印的字符数。 But in the code below why is it printing 5.但是在下面的代码中为什么打印 5。

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

It prints "1000" once and a space, so altogether we have 2 characters.它打印一次“1000”和一个空格,所以我们总共有 2 个字符。

It should output "1000 2".它应该是 output “1000 2”。 But its outputting "1000 5".但它输出“1000 5”。

The number of characters output is 5. 1000 is four characters. output 的字符数为1000为四个字符。 \n is one character. \n是一个字符。

printf doesn't return the number of "items" output like the scanf family of functions do for input. printf不会像scanf系列函数一样返回“项目”的数量 output 用于输入。 It returns the actual character count.它返回实际的字符数。

The inner call happens first, prints 5 characters ( \n , 1 , 0 , 0 , 0 ) and returns 5 .内部调用首先发生,打印 5 个字符( \n1000 )并返回5

The outer call then happens, and prints the 5 that was returned by the inner call.然后发生外部调用,并打印内部调用返回的5

suppose expression:假设表达式:

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

output output

b=10 a=4;

b because of value of assigned to b ie b因为赋值给 b ie

b=10;

and

b,=,1,0   

count as four and assigns to the a ie:计为四并分配给 a 即:

a=4;

Lets first check the output of inner printf:让我们首先检查内部 printf 的 output:

/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 返回内部 printf 的实际字符数,即 5。所以外部 printf 返回 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 printf()返回实际角色计数,在这里我们有4 (“ 1000”) + 1 (“ \ n”)字符,因此它将给出output 1000,然后是5个角色计数,是Inner ZAFA0FF8B27B27666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666abde像10005

You should clearly notice that 1000 is 4 letters and you have \n which it is a character by it self您应该清楚地注意到1000是 4 个字母,并且您有\n它本身就是一个字符

The number 1000 is composed of four digits therefore it take four characters to print it.数字 1000 由四位数字组成,因此打印它需要四个字符。 Four character plus the line feed is five characters.四个字符加上换行符是五个字符。

in printf("%d",printf("\n%d",a));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. printf("\n%d",a)将打印一个换行符'\n'和 integer 值1000 ,总共5字符。 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打印换行符和 1000,然后由外部printf打印返回值5

Printf return the number of character successfully printed by the function. Printf 返回 function 成功打印的字符数。

printf() returns total no. printf() 返回总数。 of character printed on console, you pass 1000;在控制台上打印的字符,您通过 1000; so first inner printf() function will work and print 1000,and here no.所以第一个内部 printf() function 将工作并打印 1000,这里没有。 of character is 4. One is \n.字符数是 4。一个是 \n。

So total no.所以总没有。 of character become 5, that's why it print 1000 5.字符变为 5,这就是它打印 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.正如您在图像中看到的,首先最后一个 printf 显示它应该显示的内容,即 3223433 1233。现在,最后一个 printf 返回它显示的字符串/int 中的字符数。 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. 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然后由于 2 的长度为 1,因此显示 1,最后再次显示 1,因为它是 1 的长度。

Just to add a bit more, the number of character returned by printf() may depends on the specifiers of the parameters再补充一点, printf()返回的字符数可能取决于参数的说明符

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:此代码片段中有 2 个 printf() 函数:

  1. inner printf()内部 printf()
  2. outer printf()外部 printf()

Firstly, the inner printf() will execute and will print 1000 and then the outer printf() will execute.首先,内部 printf() 将执行并打印1000 ,然后外部 printf() 将执行。

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().需要注意的是, printf()返回它打印的字符数,并且转义序列在 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.因此,通过执行内部 printf(),我们得到了 5(因为 '\n', 1, 0,0,0 是 5 个字符),现在,当执行外部 printf() 时,会打印 5。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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