The C99 standard has this to say about operator precedence in C99 6.5 Expressions /3 footnote 74
):
The syntax specifies the precedence of operators in the evaluation of an expression, which is the same as the order of the major subclauses of this subclause, highest precedence first.
Later on, we see that multiplicative operators are lumped together:
6.5.5 Multiplicative operators
Syntax
multiplicative-expression:
cast-expression
multiplicative-expression * cast-expression
multiplicative-expression / cast-expression
multiplicative-expression % cast-expression
Because they're in the same major section, their precedence is equal, and they're left-associative (a), so the expression:
45 % 54 / 5
is effectively:
(45 % 54) / 5
= (45) / 5 # 45 / 54 is 0, with a remainder of 45
= 9
That's why you get your answer of 9
.
(a) On a side note, I thought I'd found a hole in the C standard since I could find nothing to do with the associativity (left or right) of those operators, whether a/b*c
would evaluate as (a/b)*c
or a/(b*c)
.
But, it turns out, the grammar itself (see above) dictates this. Because the grammar states (in part):
multiplicative-expression:
multiplicative-expression * cast-expression
an expression like a/b*c
would have to assign c
to the cast-expression
and a/b
to the multiplicative-expression
, evaluating it first.
Hence multiplicative operators are left-associative and the ISO bods, and certain SO bods as well, have once again proved themselves cleverer than I :-)