0
int i = -5 , m,k=7;
m=2*(++i , k-=4);
printf("%d %d %d\n",i,m,k);

In this code the output is: [-4 6 3] but, I didn't quite understand where 6 comes from, can anyone help me?

DeiDei
  • 9,680
  • 6
  • 51
  • 75
gorcc
  • 35
  • 3

2 Answers2

1

k -= 4 "returns" 3.

The comma operator returns the second value which is 3.

Then you multiply 2 by 3.

m = 2 * (++i, k -= 4)
m = 2 * (-4, 3)
m = 2 * 3
m = 6
S.S. Anne
  • 14,415
  • 7
  • 35
  • 68
Florin Petriuc
  • 1,066
  • 9
  • 13
1
m=2*(++i , k-=4);

is identical to:

++i;
k -= 4;
m = 2 * k;

So you get 2 * 3 which is where you get the 6.

Please don't start thinking this sort of coding style is acceptable.

S.S. Anne
  • 14,415
  • 7
  • 35
  • 68
DeiDei
  • 9,680
  • 6
  • 51
  • 75