+ 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 slash (/) stands for two different divisions. Which division depends upon context. If the type both of its operands are char or int 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 / 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.
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.
= += -= *= /= %=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.
next: Programs 02