Arithmetic Operators

The normal arithmetic operators are provided in C. The values formed are either integer or real depending on the operand values.

+addition
-subtraction
*multiplication
/division
%modulus (remainder)

In addition the increment, ++, and decrement, --, operators are provided. These apply to discrete variables (i.e. not floating point values). They add or subtract one from their operands if these are of an integer type. If they are used with pointer variables they increase or decrease the address in the pointer by the size of one item of the type referenced.

If they are placed before their operand, the increment or decrement is performed before the value is used in the whole expression (pre-increment/decrement). More commonly they are written after their operand, so that the original alues is used this time and the increment or decrement performed at the edn of evaluation of the whole expression (post-increment/decrement).

It is sometimes confusing to determine the effect of these operators and so safer to use the longer equivalent.

The order of evaluation is generally left to right.

i = 3;
j = ++i;    /* Leaves i==4, j==4 - pre-increment */

i = 3;
j = i++;    /* Leaves i==4, j==3 - post-increment */
Example

#include 
void main() {
	/* Calculate the values of a quadratic equation */
	int X, Y;
	X = -100; 
	while (X<100) {
		Y = 5*X*X + 4*X + 12;
		printf("X = %d, Y = %d\n",X,Y);
		X++;
	}
}
Simple text version to compile.

Exercises on this section.


Next.

Back to Contents page.