Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C operator order

Tags:

c

operators

Why is the postfix increment operator (++) executed after the assignment (=) operator in the following example? According to the precedence/priority lists for operators ++ has higher priority than = and should therefore be executed first.

int a,b;
b = 2;
a = b++;
printf("%d\n",a);

will output a = 2.

PS: I know the difference between ++b and b++ in principle, but just looking at the operator priorities these precende list tells us something different, namely that ++ should be executed before =

like image 347
Jürgen Brauer Avatar asked Jul 12 '26 07:07

Jürgen Brauer


1 Answers

++ is evaluated first. It is post-increment, meaning it evaluates to the value stored and then increments. Any operator on the right side of an assignment expression (except for the comma operator) is evaluated before the assignment itself.

like image 85
owacoder Avatar answered Jul 14 '26 02:07

owacoder