An arithmetic expression contains only arithmetic operators and operands. We know that the arithmetic operators in C language include unary operators (+ - ++ -- ), multiplicative operators (* / %) and additive operators (+ - ). The arithmetic operands include integral operands (various int and char types) and floating-type operands (float, double and long double).
Example Simple arithmetic expressions
a) Several valid arithmetic expressions are given below. Assume that variables a and b are of type int.
Observe that the operators are used correctly. The unary operators in expressions -1.23, a++ and -a + b /5 correctly operate on a single operand. The unary minus (-) is used as a prefix operator whereas the increment operator (++) is used in the postfix form. All other operators (multiplicative and additive) are used as infix operators and they operate on two operands. Also, the modulo-arithmetic(s) operator operates on integer operands as required. The expression 'A' + 2 is valid and its value is equal to 67, as the ASCII value of character 'A' is 65. Note that the operands and operators need not be separated by spaces. For example, the last two expressions in the first row can also be written as -a+b/5 and 3 .142*a*b. However, the spaces are often used to improve readability.
b) The expressions given below are invalid
The first two expressions are invalid because the operands a and b are not connected by any operator. This is true even for the third expression (a++ --b) as the++ and -- operators operate on operands a and b, respectively, but there is no operator connecting these operands.
The last two expressions are invalid as they contain two binary operators in succession. These operators operate on a single operand instead of two. Note that the - operator in last expression cannot be interpreted as a unary operator as it does not precede an operand.
c) Can you tell which of the following expressions are valid?
Surprisingly, all are valid C expressions although they do not appear to be so. In expression +a +b, the first+ operator is a unary operator that operates on variable a, whereas the second+ is a binary operator that connects variables a and b.
Now observe the space between the two'-' symbols in the second expression. They cannot
be interpreted as a decrement operator. As a result, the second - symbol is interpreted as a unary minus operator, whereas the first - is interpreted as a subtraction operator that operates on two operands a and -b. ·
The two consecutive operators in expression a % 10 I - b does not pose a problem as the -
operator is interpreted as a unary operator and the I operator operates on two operands (a % 10 and -b). However, the space between - and b is misleading.
In the last expression, observe the spaces separating the middle + symbol from the increment operators that operate on variables a and b. The middle + is interpreted as a binary operator that operates on a and b.
d) Several simple mathematical equations are given below along with equivalent C expressions.
Assume that the symbolic constant PI has been defined as
#define PI 3.1415927
Observe that the literal 4 in expression 3 is written as floating constant 4.0 to avoid the integer division of 4 and 3. Similarly, constant 9 in expression 4 is also written as a floating constant. The sum of digits of a two-digit integer number is written as n I 10 + n % 10.Since n is assumed to be an integer variable, the expression n I 1o performs integer division and determines the digit at the 10's place, whereas the expression n % 10 determines the digit at the unit's place.
Evaluation of Simple Arithmetic Expressions
We use the operator precedence and associativity rules to determine the meaning and value of an expression in an unambiguous manner. Recall that the operators in an expression are bound to their operands in the order of their precedence. If the expression contains more than one operator at the same precedence level, they are associated with their operands using the associativity rules. Table summarizes these rules for arithmetic and assignment operators.
If the given expression is simple, we can often directly convert it to its mathematical form and evaluate it. However, if the given expression is complex, i. e., it contains several operators at different precedence levels, we need a systematic approach to convert it to a mathematical equation and evaluate it. The steps to convert a given valid C expression to its mathematical form (if possible) and to evaluate it are as follows:
1. First determine the order in which the operators are bound to their operands by applying the precedence and associativity rules. Note that after an operator is bound to its operand(s), that sub-expression is considered as a single operand for the adjacent operators.
2. Obtain the equivalent mathematical equation for given C expression (if possible) by following the operator binding sequence (obtained in step 1).
3. Determine the value of the given expression by evaluating operators in the binding sequence.
The steps to determine operator binding in an arithmetic expression are explained below with the help of the expression -a+ b * c - d I e + f.
1. The unary operators (unary +, unary - , ++ and - -) have the highest precedence and right-to-left associativity. Thus, the given expression is first scanned from right to left and unary operators, if any, are bound to their operands. The order is indicated below the expression as follows:
2. The multiplicative operators (*, I and %) have the next highest precedence and left to- right associativity. Thus, the expression is scanned from left-to-right and the multiplicative operators, if any, are bound to their operands as shown below. (Observe that after completion of the above step, sub-expressions -a, b * c and d I e will be operands for the remaining operator bindings.)
3. The additive operators (+ and -) have the next highest precedence and left-to-right associativity. Hence, the expression is scanned from left-to-tight and the additive operators, if any, are bound to their operands as shown below. Observe that the operands for the first + operator are the sub-expressions -a and b * c. Similarly, the operands for the - operator are -a+ b * c and d /e.
Now we can write the mathematical equation for the given C expression by following the operator binding sequence as shown below:
Now the given expression can be evaluated, again by following the operator binding sequence, as shown below. Assume that the variables a, b, c, ct, e and f are of type float and are initialized with values as a= 1. 5, b = 2. 0, c = 3. 5, ct= 5.0, e = 2. 5 and f = 1. 25.
Remember that except for some operators (& & || ? : and the comma operator), the C language does not specify the order of evaluation of sub-expressions. Thus, the sub expressions at the same level can be evaluated in any order. For example, the sub expressions -a, b * c and ct I e in the above expression can be evaluated in any order.
The procedure explained above can also be used to check the validity of an expression. The given expression is valid if we arrive at a single operand (or value) after all the operators in the given expression are considered. Consider a more complex arithmetic expression: -a--+ -b++ * --c. This expression appears to be invalid due to the excessive use of operators. It contains three operands a, b and c and seven operators, five of which are unary (-, ++ and --) and the other two are binary operators (+ and *). However, using the operator binding steps, we can easily verify that it is a valid expression: