Search code examples
c++mathoperatorsexecution

Order of arithmetic operator execution C++


In what order are the arithmetic operators (+,-,*,/,%) executed in C++? Is the standard BODMAS rule applicable here? As an example, what would be the value of m here:

m = 605 / 10 + 45 % 7 + 29 % 11;

Solution

  • The operator precedence of C++ is the standard mathematical precedence, where % has the same precedence as /.

    So, the expression m = 605 / 10 + 45 % 7 + 29 % 11; would be evaluated as

    m = (605 / 10) + (45 % 7) + (29 % 11);
    

    Which would result in:

    m = (605 / 10) + (45 % 7) + (29 % 11);
    m = 60 + 3 +7;
    m = 70;