read: Precedence of Operators: The C programming Lang, ยง2.12, p48-49

Arithmetic Expressions And Operators

C evaluates expressions. Evaluating an expression means finding its value.
  1. The arithmetic operators below apply to the numeric types integers: char, short, int, long and floating point: float, double. The numbers the operators operate on are called operands.
    +   addition
    -   subtraction
    *   multiplication
    /   division
    %   remainder
    
    The C language allows you to mix different numeric types. Consider the expression:
    2+3.0
    
    The int 2 is "promoted" to 2.0 so the result is 5.0. As is standard in math classes multiplication and division take precedence over addition and subtraction. Thus the value of
    2+3*5
    
    is 17 not 25. The arithmetic operators are left associative. Suppose an expression contains two arithmetic operators of equal precedence. The left operator will be evaluated first. For example, the value of the expression
    9-3-1
    
    is 5 NOT 7. The left subtraction is done first.

  2. Examples of right associative operators are the assignment operators. Consider the expression below where variable y has been declared int and variable x has been declared float.
    x = y = 11.5
    
    The right hand = is done first. The value 11 is assigned to variable y. The .5 is dropped because y is an int variable. The value of
        y = 11.5
    
    in the whole expression above is the value stored in y, the 11. Now the left assignment assigns the 11 to variable x which is a floating point variable. So the value stored is 11.0
  3. The slash (/) stands for two different divisions. Which division depends upon context. If the type both of its operands are integer then integer division is performed. If at least one of slash's operands is floating point then floating point division is done. Integer division returns an integer: the whole number of times the divisor goes into the dividend.
    dividend / divisor
    
    If we have 3/5 the computer does integer division: the result is 0. There are zero 5's in 3. If we have 3.0/5, the computer does floating point division: the result is 0.6. Let's look at another example:
    2.0*3/5
    
    What are the operands of the slash? Since the multiplication is done first, the operands of the slash are 6.0 and 5. So floating point division is done and the result is 1.2. Note if you want some operation done first you put it in parentheses. Here is a slight variation on the last expression:
    2.0*(3/5)
    
    Now the division is done first. It is integer division since 3, 5 are ints. So its value is 0. So the value of the whole expression is 0.0.
  4. The % operator is the remainder operator. It goes with integer division. 25%7 is 4 since 7 goes into 25 3 times with a remainder of 4.
  5. Assignment Operators. Assignment is about assigning a value to variable. That's a fancy way of saying storing a value in a variable. Some of the assignment operators are:
    =     
    +=
    -=
    *=
    /=
    %=
    
    All of the assignment operators expect a storage location on the left (like a variable or a place in an array) and an expression on the right. Assignment operators have very low precedence so the expression on the right is evaluated before the assignment takes place.

    Example: Let x be an int variable.

    x = x+2;
    
    Since = has low precedence the value of x+2 is computed first. Then the assignment operator assigns that value to variable x. The net effect is that x is now 2 units larger. The whole thing, x=x+2, is itself an expression; its value is the value put into x. An alternate way of doing the same thing is:
    x += 2;
    
    The value of the right hand side is 2. The += operator changes the content of x by adding this 2 to the value in x.

    Example:

    x *= 3+2;
    
    Since *= has low precedence we calculate the value of the right hand side. The value is 5. Now we do the *= operation. We change variable x by multiplying it by 5. As above, the whole thing, x *= 3+2, is an expression. Its value is the value stored in x by the *= operation.
  6. WARNING: Precedence and associativity rules do NOT tie everything down. Badly written code can result in different C compliers producing different results. Example:
       a[i] = i++;
    
    Here the operators are: [ ], =, and ++. The assignment operator, =, is adjacent (next to) the other operators and it has lower precedence the other two. So it will be done last. But precedence does not speak to which of [ ] or ++ will be done first. These operators are separated by the assignment operator. They are not adjacent. If the left hand side is evaulated first, the value on the right will be put into the array at the old index. On the other hand if the compiler does the right hand side first, the value will be put at the new index, one larger than the old index. The mistake here is that both sides use variable i and one side changes the value in i.

next: Number Bases