int main()
{
int a, b, c;
a = 10;
b = 20;
c = printf("%d", a) + ++b;
printf("\n%d", c);
}
The output of the above program is 23 it seems but i dont know how it is obtained. Can anyone have an idea about it?
printf has a return value, which is the total number of characters it prints.
The statement printf("%d",a) will print 10, which means the return value of printf here is 2.
The rest is easy:
c=printf("%d",a)+ ++b;
c will have a value of 2 + 20 + 1, which is 23.
c=printf("%d",a)+ ++b;Here the output will be two different integers,for two different printf statements . For the first printf statement the code prints 10, then when this printf statement participate in some assignment statement , it is treated as the number of characters it is printing i.e. 2 here. Then it is added to ++b i.e. 21 (PRE-INCREMENTED) . So the output is 23(2 + 21) .
The whole output looks like this :
10
23