Next: Comparison Operators Up: Basic C Programming Previous: Printing Out and Inputting Variables

Arithmetic Operations

As well as the standard arithmetic operators () found in most languages, C provides some more operators. There are some notable differences with other languages, such as Pascal.

Assignment is = i.e. ; ch = `y';

Increment ++, Decrement which are more efficient than their long hand equivalents, for example:- x++ is faster than x=x+1.

The ++ and operators can be either in post-fixed or pre-fixed. With pre-fixed the value is computed before the expression is evaluated whereas with post-fixed the value is computed after the expression is evaluated.

In the example below, ++z is pre-fixed and the w is post-fixed:

This would be equivalent to:

The % (modulus) operator only works with integers.

Division / is for both integer and float division. So be careful.

The answer to: is 1 even if is declared a float!!

RULE: If both arguments of are integer then do integer division.

So make sure you do this. The correct (for division) answer to the above is or or (better) .

There is also a convenient shorthand way to express computations in C.

It is very common to have expressions like: or

This can written in C (generally) in a shorthand form like this:

which is equivalent to (but more efficient than):

So we can rewrite as

and as .

NOTE: that means and NOT .


Dave.Marshall@cm.cf.ac.uk
Wed Sep 14 10:06:31 BST 1994