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

时间:2011-01-30 09:32:51

标签: c

#include<stdio.h>

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

输出如下:2 2 2 2 3

我无法弄清楚为什么输出是这样的。有什么想法吗?

1 个答案:

答案 0 :(得分:15)

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"