C语言 C 中 printf() 函数的返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7055882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:23:26  来源:igfitidea点击:

Return value of printf() function in C

c

提问by Angus

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

printf()函数将返回打印的字符数。但是在下面的代码中,为什么要打印 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". But its outputting "1000 5".

它应该输出“1000 2”。但它输出“1000 5”。

回答by Mat

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

输出的字符数是5。1000是四个字符。\n是一个字符。

printfdoesn't return the number of "items" output like the scanffamily of functions do for input. It returns the actual character count.

printf不会像scanf函数族为输入那样返回“项目”输出的数量。它返回实际的字符数。

回答by RichieHindle

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

内部调用首先发生,打印 5 个字符(\n, 1, 0, 0, 0)并返回5

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

然后外部调用发生,并打印5内部调用返回的 。

回答by Ranjit Pasale

suppose expression:

假设表达式:

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

output

输出

b=10 a=4;

bbecause of value of assigned to b i.e

b因为赋值给 b ie

b=10;

and

b,=,1,0   

count as four and assigns to the a i.e:

算作四并分配给 a 即:

a=4;

回答by abe312

Lets first check the output of inner printf :

让我们首先检查内部 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 返回内部 printf 的实际字符数,即 5。因此外部 printf 返回 5。

回答by Qchmqs

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

您应该清楚地注意到这1000是 4 个字母,并且您拥有\n它本身就是一个字符

回答by Jdpurohit

printf() returns the actual character countwhere 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") 个字符,所以它会给出输出 1000 然后是 5,这是内部 printf 函数的字符数,它看起来像10005

回答by Opera

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.

数字 1000 由四位数字组成,因此打印它需要四个字符。四个字符加上换行符是五个字符。

回答by phoxis

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

printf("%d",printf("\n%d",a));所述printf("\n%d",a)将打印一个新行字符'\n'和整数值1000,这使得总的5字符。首先printf调用第一个内层,打印换行符和 1000,然后5由外层打印返回值printf

回答by user6741889

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.

printf() 返回总数。控制台上打印的字符数,您通过 1000;所以第一个内部 printf() 函数将工作并打印 1000,这里没有。字符数为 4。一个是 \n。

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

所以总没有。字符变成 5,这就是为什么它打印 1000 5。

回答by kapilddit

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

printf 返回函数成功打印的字符数。