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
  • 10,205
  • 6
  • 55
  • 80
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
  • 15,171
  • 8
  • 38
  • 76
Florin Petriuc
  • 1,146
  • 9
  • 16
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
  • 15,171
  • 8
  • 38
  • 76
DeiDei
  • 10,205
  • 6
  • 55
  • 80