Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't understand x *= y+1 output

Tags:

c

I have a problem understanding the output of the code. Any explanation please...

#include<stdio.h>
void main()
{
     int x=2,y=5;
     x*=y+1;
     printf("%d",x);
}

The output is as 12. But as per my understanding x*=y+1;is x=x*y+1; but as per operator precedence x*y should be evaluated followed by adding 1 so it should be 10+1=11. But it is 12 — can anyone explain please?

like image 778
shashank Avatar asked Nov 28 '22 13:11

shashank


1 Answers

It will be evaluated as

x = x * (y + 1);

so

x = 2 * ( 5 + 1 )
x = 12
like image 113
sujithvm Avatar answered Dec 16 '22 11:12

sujithvm