+ addition - subtraction * multiplication / division % remainderThe C language allows you to mix different numeric types. Consider the expression:
2+3.0The 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*5is 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-1is 5 NOT 7. The left subtraction is done first.
y
has been declared
int
and variable
x
has been declared
float
.
x = y = 11.5The 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.5in 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
dividend / divisorIf 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/5What 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.
= += -= *= /= %=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.
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