ECE-1021 Lesson 1 

Arithmetic Operators

(Last Mod: 27 November 2010 21:38:39 )

ECE-1021 Home



Objectives


Overview

These operators perform the standard arithmetic operations that most people are long familiar with. Each operator exists in two forms - the normal operator and the abbreviated assignment operator. Both are binary operators requiring two operands. In the case of the abbreviated assignment versions, the left operand must be a modifiable object - i.e., an lvalue.

In addition, two special unary versions are available for addition and subtraction. These operators, the increment and decrement operators, require an lvalue as an operand. The operator can either prefix the operand or postfix the operand. If it prefixes the operand, then the expression evaluates to the value that will eventually be written to the operand. If it postfixes the operand, then the expression evaluates to the original value of the operand.

Operation Operator Level Assoc Syntax Evaluates to
Unary Plus + 2 R +A A
Unary Minus - 2 R -A -A
Multiplication * 3 L A * B A * B
*= 14 R lvalue *= B lvalue * B
Division / 3 L A / B A / B
/= 14 R lvalue /= B lvalue / B
Modulus % 3 L A % B A % B
%= 14 R lvalue %= B lvalue % B
Addition ++ 2 R ++lvalue lvalue+1
lvalue++ lvalue
+ 4 L A + B A + B
+= 14 R lvalue += B lvalue + B
Subtraction -- 2 R --lvalue lvalue-1
lvalue-- lvalue
- 4 L A - B A - B
-= 14 R lvalue -= B lvalue - B

Notes on the Division Operator

If BOTH operands for the division operator are of integer type, then integer division is performed. This means that, regardless of the values of the operands, the result will be an integer equal to the algebraic quotient with the fractional part discarded. This is known as "truncation towards zero".

If the divisor is zero, then the behavior is undefined.

Examples:

quotient = dividend / divisor;

quotient =  29 / 10; /* Algebraic Result is  2.9, Expression Result is  2

quotient = -29 / 10; /* Algebraic Result is -2.9, Expression Result is -2


Notes on the Modulus Operator

The modulus operator is only defined if both operands are of integer type. Like division, if the divisor is zero the behavior is undefined.

The result of the expression is defined as the remainder after integer division. Hence we can use this definition to find the result of the modulus operation:

remainder = dividend % divisor;

Must produce the same result as:

quotient = dividend / divisor;

remainder = dividend - (divisor * quotient);

Another consequence of this definition for modulus is:

dividend  = (divisor * quotient) + remainder;

m = n*(m/n) + (m%n);


Notes on Pointer Arithmetic

 

If any of the operands to any of the functions are pointers, then the following constraints apply:

 

These constraints greatly limit the number of legal combinations of operators and operands:

 

Pointer Operation Operand Types Result Type Result
Pointer Addition (Pointer) + (Integer) pointer Address of object
(Integer) + (Pointer) pointer Address of object
Pointer Subtraction (Pointer) - (Integer) pointer Address of object
(Pointer) - (Pointer) integer Distance between objects

All pointers except void pointers have a type. The expression sizeof(type) returns the number of bytes required to store one value of that type. The integer value used in pointer arithmetic expressions is interpreted as being the number of elements, not the number of bytes, to be added to or subtracted from the address stored in the pointer. Hence the compiler multiplies the value of the integer operand by the size of the type of element pointed to to yield an offset in bytes. This is the value that is added to the value of the address stored in the pointer.

When subtracting two pointers, the result is similarly to be interpreted as the number of elements, not the number of bytes, separating the two objects that are pointed to. Hence the value that is obtained by direct subtraction of the two addresses gives the number of bytes between the two addresses pointed to and the value of the expression is this value divided by the size of the elements pointed to. This convention requires that both pointers point to the same type of data.

Pointers that are of type void have no type information associated with them. This means that the compiler has no way of determining how many bytes are required to store each element. As a result, pointer arithmetic cannot be performed on void pointers.