简体   繁体   English

为什么printf会在我的程序中输出?

[英]Why does printf output this in my program?

#include<stdio.h>

int main()
{
    printf("%d",printf("%d %d",2,2) & printf("%d %d",2,2));
}

The output comes like this: 2 2 2 2 3 输出如下: 2 2 2 2 3

I cannot figure out why the output is like this. 我无法弄清楚为什么输出是这样的。 Any ideas? 有任何想法吗?

printf returns the numbers of characters printed, so here is the explanation: printf返回打印的字符数,所以这里是解释:

printf("%d",printf("%d %d",2,2) & printf("%d %d",2,2));
\_________/ \_________________/ | \_________________/
     |              |           |          |
     |         prints "2 2"     |    prints "2 2"
     |         and returns 3    |    and returns 3
     |                          |
     |                    computes 3 & 3
     |                   (which equals 3)
     |
 prints "3"

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

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