Arithmetic - Expressions

Chapter chap3 section 2

In the previous chapter we saw how we could the computer to add up two numbers by writing the expression

x+y

The appearance of such an expression causes the computer to calculate the value of the expression when executing the statement that includes the expression. It also seems obvious that one can add up three numbers using an expression such as

x+y+z

In both these expressions are both variables such as "x", "y" and "z" and the symbol "+". The symbol "+" means add up the two surrounding numbers. It is one of several elementary operators .

+       meaning add
-       meaning subtract
*       meaning multiply
/       meaning divide

The reason that "*" is used for multiplication rather than an "X" or plain juxtaposition of variables as is done in ordinary algebraic notation, is that an "X" could easily be confused with the name of a variable and juxtapostion of two variable names looks like a third variable name.

Strictly the operators shown above are binary operators which means that they operate on two numbers. The values associated with an operator may be

  1. actual numbers called constants

  2. the names of variables, in which case the value of the variable is intended

  3. expressions, in which case the value of the expression is intended

The following are all valid expressions assuming x,y and z are variables

  1. x+23
  2. 4500
  3. x+y*11
  4. z

The following program shows some simple examples of the use of the operators shown above.
main()
{
	int	x = 3;
	int	y = 2;
	int	z = 6;
	printf("This is a constant -- %d\n",213);
	printf("The value of %d-%d is %d\n",x,y,x-y);
	printf("The value of %d*%d is %d\n",x,y,x*y);
	printf("The value of %d/%d is %d\n",z,y,z/y);
}
It produced the following output
This is a constant -- 213
The value of 3-2 is 1
The value of 3*2 is 6
The value of 6/2 is 3

The value of any expression involving integers will itself be an integer. This is straightforward for addition, subtraction and multiplication but division requires further consideration. Division of two integers results in an integer valued quotient and a remainder that plays no further part in the proceedings.

Alternatively you can regard division as producing an answer including a fractional part and the resultant number being truncated by discarding the fractional part. The ANSI standard talks about truncation although all computers actually do integer division and discard the remainder.

So when 20/7 is calculated, the result can be thought of either as 2 remainder 6 or as 2.85714..... In the first case discarding the remainder gives the result 2 and in the second case truncating the result also gives the result 2.

The ANSI standard says that when two positive integers are divided, the result is truncated towards zero. If the division involves negative numbers then the result may be truncated up or down.

The following program shows the values of some actual quotients

main()
{
	int	x = 5;
	int	y = 8;
	int	p = -3;
	int	q = -5;
	printf("The value of %2d divided by %2d is %2d\n",
			y,x,y/x);
	printf("The value of %2d divided by %2d is %2d\n",
			y,p,y/p);
	printf("The value of %2d divided by %2d is %2d\n",
			p,q,p/q);
	printf("The value of %2d divided by %2d is %2d\n",
			q,x,q/x);
}
When compiled and run the program produced the following output.
The value of  8 divided by  5 is  1
The value of  8 divided by -3 is -2
The value of -3 divided by -5 is  0
The value of -5 divided by  5 is -1
Although the ANSI standard allows computers to give either -2 or -3 as the value of 8/-3, all three systems tested, when writing these notes, gave the result -2.

The effects of attempting to divide by zero are officially undefined . The ANSI standard does not require compiler writers to do anything special, so anything might happen. Of course we tried this by changing the value of x to zero in the previous program. Turbo C spotted what was going on and displayed the message

Divide error

The Unix systems were slightly less informative producing the following messages

Arithmetic exception (core dumped)
Breakpoint - core dumped

on the SUN Sparc station and IBM 6150 respectively. Both Unix systems produced the core file described in the previous chapter.

Whether it is reasonable to expect the computer to check every division operation by examining the divisor before actually executing the division is a debatable point, unless there is special hardware for detecting the condition it can slow programs down.


Evaluation of Expressions