Bug in C compiler (?)
The following code prints 25. Shouldn't it be printing 20? int a = 3; printf("%d", ++a * ++a); // prints 25 I've also tested it in java compiler. It prints 20! int a = 3; System.out.print( ++a * ++a ); // prints 20 (java) Why is C compiler printing 25?? EDIT: I don't think the "operator precedence" explains this, because, in java, ++ also have higher precedence. Having a higher precedence only means that the ++ will be done first, so: 1) increment a, then use its value (use a = 4) 2) increment a again, then use its value (use a = 5) 3) multiply the results: 4 * 5 = 20 EDIT2: "All increments have to be done before the multiplication. First increment increases a to 4, then it is increased to 5. Then the multiplication takes place, and by that time, a has the value 5". Not necesseraily. If you use post-increment instead, it will not use equal values. The following code prints 12 (3*4). By the same logic, it should print 16 (4*4)... Or not, I don't know, it's very confusing >_< int a = 3; printf("%d", a++ * a++ ); // prints 12 And another thing: if all increments are done first and then the final value is used, so, the following code should print 216 (6*6*6), but it prints 150 (5*5*6) instead. int a = 3; printf("%d", ++a * ++a * ++a ); // prints 150 W H Y ?