1. ## C program :Calculation

I fail to understand why this program prints -8

Code:
```#include <stdio.h>

int main(void) {

int x=2;
int y=3;
int z=4;

x*=-2*(y+z)/3;
printf("result %d",x);

return 0;
}

my understanding :
x*=-2*(y+z)/3;
=> x=x*{-2*(y+z)/3}
=>x=2*{-2*(3+4)/3}
=>x=2*{-2*7/3}
=>x=2*{-2*2.33333....}
=>x=2*(-4.66666666667)
=>x=-9.33333333334```
since x is intger ..I expect it to print -9 here (leaving the fractional parts)

But it prints -8

How ?

2. You have to do everything in the right order and you have to do each operation as an integer operation, truncating as you go:

x = x * (-2 * (y + z) / 3)
2 * (-2 * (3 + 4) / 3)
2 * (-2 * 7 / 3)
2 * ( -14 / 3)
2 * -4
-8

3. You have to do everything in the right order
what is the right order?

does it not follow Math rules? in Math rules you Divide first and then multiply.

So as per Math rule it should be
(-2 * 7 / 3)
=> -2*(7/3) //divide first

But you wrote
(-2 * 7 / 3)
=> -14/3

you multiplied first.

hence the confusion.

Could you please clarify this part.

4. You also need to be aware of C operator precedence and associativity.
C Operator Precedence - cppreference.com

After the ( ) have been resolved, the -2 * 7 / 3 part is evaluated left to right, since both * and / have equal precedence.

5. Originally Posted by Stream
does it not follow Math rules? in Math rules you Divide first and then multiply.
That is not a rule in mathematics... just sayin'

6. It's nothing to do with order, multiply or division first will get the same result; the reason result became -8 is about type conversion, division operator doesn't always return float type.

7. Originally Posted by Hodor
That is not a rule in mathematics... just sayin'
why not ? Its the Math rule ..from left to right ....divide first and multiplication second.
see this

Source: Ordering Mathematical Operations, BODMAS | SkillsYouNeed

8. Originally Posted by Stream
Its the Math rule ..from left to right ....divide first and multiplication second.
The exact rules depend on the precedence conventions adopted, even amongst mathematicians, e.g., some might rank division as having higher precedence, others might rank division and multiplication as having equal precedence, and the precedence convention may differ where the multiplication is implicit rather than expressed with an explicit operator.

Anyway, while that is interesting mathematical trivia, in programming we have something similiar: the rules of precedence are determined by the rules of the language, hence it can differ amongst different programming languages, and does not need to follow any mathematical convention, although typically programming languages try to keep to some mathematical convention in order to make use of the mathematical background knowledge of programmers and would-be programmers. Hence, C has precedence rules that place division and multiplication as having equal precedence, because that is a convention from mathematics, even if the convention that you learnt is different.

Originally Posted by Stream
Note that your source actually states the other convention, i.e., "Multiplication and division rank equally".

9. Originally Posted by Stream
why not ? Its the Math rule ..from left to right ....divide first and multiplication second.
see this

Source: Ordering Mathematical Operations, BODMAS | SkillsYouNeed
They improved BODMAS by adding color, but it is still a subtile hint--division and multiplication are equal precedence, as are addition and subtraction.

10. In an alternative reality, BOMDAS rules and we're discussing multiplying first.