user507401
user507401

Reputation: 2899

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

I cannot figure out why the output is like this. Any ideas?

Upvotes: 2

Views: 191

Answers (1)

aioobe
aioobe

Reputation: 421170

printf returns the numbers of characters printed, so here is the explanation:

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"

Upvotes: 15

Related Questions