170 likes | 341 Views
4. EXPRESSIONS. Arithmetic Operators. The arithmetic operators are: + unary plus - unary minus + addition - subtraction * multiplication / division % remainder (modulus). Arithmetic Operators.
E N D
Arithmetic Operators • The arithmetic operators are: + unary plus - unary minus + addition - subtraction * multiplication / division % remainder (modulus)
Arithmetic Operators • The value of i % j is the remainder when i is divided by j. For example, the value of 10 % 3 is 1; the value of 12 % 4 is 0. The % operator requires integer operands; all other operators allow either integer or floating-point operands. • If both operands are integers, / truncates the result toward 0. • There is no exponentiation operator. • Must use “pow” function defined in <math.h>
Operator Precedence • When an expression contains more than one operator, the meaning of the expression may not be immediately clear: Does i + j * k mean (i + j) * kor i + (j * k) ? • In C, this potential ambiguity is resolved by operator precedence. • Precedence of the arithmetic operators: Highest: + - (unary) * / % Lowest: + - (binary)
Operator Precedence • Examples: i + j * k means i + (j * k) -i * -j means (-i) * (-j) +i + j / k means (+i) + (j / k)
Associativity • Operator precedence rules alone aren’t enough when an expression contains two or more operators at the same level of precedence. The associativity of the operators now comes into play. • The binary arithmetic operators are all left associative (they group from left to right); the unary operators are right associative. • Examples of associativity: i - j - k means (i - j) - k i * j / k means (i * j) / k i - j * i + k means (i - (j * i)) + k - - i means -(-i)
Increment and Decrement Operators • The ++ and -- operators increment and decrement variables. • Both operators have the same precedence as negation. • Either operator can be prefix or postfix: ++i i++ --i i-- • When used as a prefix operator, ++ increments the variable before its value is fetched: i = 1; printf("i is %d\n", ++i); /* prints "i is 2" */
Increment and Decrement Operators • When used as a postfix operator, ++ increments the variable after its value is fetched: i = 1; printf("i is %d\n", i++); /* prints "i is 1" */ printf("i is %d\n", i); /* prints "i is 2" */ • The -- operator has similar properties: i = 1; printf("i is %d\n", --i); /* prints "i is 0" */ i = 1; printf("i is %d\n", i--); /* prints "i is 1" */ printf("i is %d\n", i); /* prints "i is 0" */
Lvalues • The increment and decrement operators require an lvalue (pronounced “Lvalue”) as their operand. • An expression has an lvalue if it represents a storage location in computer memory. • Variables are lvalues, since a variable is just a name for a storage location. • Constants and most expressions containing operators are not lvalues, including the following: 12 i + j -i
Simple Assignment • One way to change the value of a variable is by assignment. • Simple assignment has the form v = e, where v is an lvalue and e is an expression. Examples: i = 5; /* i is now 5 */ j = i; /* j is now 5 */ k = 10 * i + j; /* k is now 55 */ • If v and e don’t have the same type, then the value of e is converted to the type of v before assignment: int i; float f; i = 72.99; /* i is now 72 */ f = 136; /* f is now 136.0 */
Simple Assignment (Continued) • Assignment is a true operator, just like + and *. The value of v = e is the value of v after the assignment. • Assignments can be chained together: i = j = k = 0; • Since the = operator is right associative, this statement is equivalent to i = (j = (k = 0)); • The assignment operator may appear in any expression: i = 1; k = 1 + (j = i); printf("%d %d %d\n", i, j, k); /* prints "1 1 2" */ • Hiding an assignment inside a larger expression usually isn’t a good idea.
Compound Assignment • In addition to the simple assignment operator =, C provides ten compound assignment operators, including these five: += -= *= /= %= • The expression v += e is equivalent to v = v + e, except that v is evaluated only once in the former expression. The other operators have similar meanings.
Compound Assignment Examples: i = 5; j = 2; i += j; /* i is now 7 */ i = 5; j = 2; i -= j; /* i is now 3 */ i = 5; j = 2; i *= j; /* i is now 10 */ i = 5; j = 2; i /= j; /* i is now 2 */ i = 5; j = 2; i %= j; /* i is now 1 */
Compound Assignment • The compound assignment operators have the same properties as the = operator; in particular, they’re right associative and can be used in arbitrary expressions.
Expression Statements • Any expression can be used as a statement: ++i; • The value of the expression is discarded; the statement has no effect unless the expression modifies one of its operands: i = 1; /* stores 1 into i, then evaluates i and discard the result */ i--; /* fetches i, discards the result, then decrements i */ i + j; /* adds i and j, then discards the result */
Expression Statements • Expression statements are typically used to perform assignments or to increment or decrement variables. • Warning: A minor typing mistake can turn a meaningful statement into a meaningless expression statement. For example, the statement i = j; could easily become i + j;