Arithmetic - Evaluation of Expressions

Chapter chap3 section 3

The expression

x+y+z

appeared earlier. This expression looks innocent but illustrates an important point. Since the operator "+" is a binary operator this expression has to be evaluated in 2 steps. These could be either

Evaluate the expression x+y
Add z to the result of the previous evaluation

or

Evaluate the expression y+z
Add x to the result of the previous evaluation

The ANSI standard allows computers to do it either way and it clearly makes no difference to the result. However for the expression

x-y+z

it clearly does make a difference as the following quick calculation shows.

First assume x has the value 1, y has the value 2 and z has the value 3. Evaluating x-y and then adding z to the result gives the value +2 whereas evaluating y+z and then subtracting the result from x gives the value -4.

The ANSI standard is quite definite about what should happen. It states that the "+" and "-" operators group or associate left-to-right. This means that an expression such as x-y+z is evaluated from left to right corresponding to the first alternative described above. If we weren't certain what this meant, a quick program confirms that our compiler writer interpreted it the same way as we did.

main()
{
	int	x=1,y=2,z=3;
	printf("Value of \"%d-%d+%d\" is %d\n",
		x,y,z,x-y+z);
	printf("value of \"%d+%d-%d\" is %d\n",
		x,y,z,x+y-z);
}
Giving the following output
Value of "1-2+3" is 2
value of "1+2-3" is 0
Suppose now that we did want to calculate y+z first and then subtract the result from x. This problem can be solved by using an extra piece of notation and writing the expression as

x-(y+z)

The parentheses (jargon for round brackets) enclose an expression and expressions enclosed within parentheses are evaluated before other expressions according to the standard. Further expressions contained within parentheses can be enclosed within parentheses to an arbitrary depth, this is called expression nesting. Expressions enclosed within parentheses are sometimes called sub-expressions but this isn't really very helpful as they are proper expressions in their own right. Again a simple programming example is sufficient to convince ourselves that parentheses work as advertised.

main()
{
	int	x=1,y=2,z=3;
	printf("Value of \"%d-(%d+%d)\" is %d\n",
		x,y,z,x-(y+z));
}
Resulting in the output
Value of "1-(2+3)" is -4
Notice how we used escaped double quotes within the printf() layout specification to ensure that actual double quotes appeared in the output.

The ANSI standard rather grandly calls parentheses primary grouping operators.

There is an important difference between the "+" and the "-" operators. The jargon says that "+" operator is a commutative operator whereas - is not. The adjective commutative applied to a binary operator means that it doesn't matter which order it takes its operators in. In other words

value1 operator value2

has exactly the same value as

value2 operator value1

This is clearly true for "+" and equally clearly false for "-".

The ANSI standard says that expressions involving only one of the commutative and associative operators can be evaluated in any order. An associative operator is an operator which has the property

value1 operator ( value2 operator value3 )

has the same value as

( value1 operator value2 ) operator value3

allowing one to write, unambiguously,

value1 operator value2 operator value3

The only associative and commutative operators we have met so far are "+" and "*".

You may wonder if it could possibly matter how x+y+z is calculated. The answer is that it might matter if an arithmetic overflow occurred during the calculation, the problems of arithmetic overflow will be discussed, briefly, later in this chapter.


Operator Precedence