Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does printf output this in my program?

Tags:

c

#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?

like image 717
user507401 Avatar asked Jul 20 '26 22:07

user507401


1 Answers

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"
like image 142
aioobe Avatar answered Jul 23 '26 13:07

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!