简体   繁体   English

为什么这个嵌套的 printf 语句打印“5 53”?

[英]Why does this nested printf statement print "5 53"?

#include <stdio.h>

int main() {
    int x=5;
    printf("%d", printf("%d %d",x,x));
}

How is the output 5 53 can someone explain ?输出5 53谁能解释一下? I tried myself and I think the answer should be 5 5 2我自己试过,我认为答案应该是5 5 2

The return value of printf is the printf的返回值是

number of characters transmitted to the output stream or negative value if an output error or an encoding error (for string and character conversion specifiers) occurred如果发生输出错误或编码错误(对于字符串和字符转换说明符) ,则传输到输出流的字符数或负值

(from here , emphasis mine) and "5 5" are three characters (namely "five space five"). (从这里开始,强调我的)和“5 5”是三个字符(即“五个空格五”)。 So the last number printed is 3.所以最后打印的数字是 3。

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

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