Site hosted by Angelfire.com: Build your free website today!



Next: Comparison Up: Expressions and Operators Previous: Arithmetic operators

Type conversion

You can mix the types of values in your arithmetic expressions. char types will be treated as int. Otherwise where types of different size are involved, the result will usually be of the larger size, so a float and a double would produce a double result. Where integer and real types meet, the result will be a double.

There is usually no trouble in assigning a value to a variable of different type. The value will be preserved as expected except where;

Where a function expects an argument of one type, corruption will occur if the wrong type is supplied. It is possible to use a technique called casting to temporarily disguise the argument as the correct type.

eg. The function sqrt finds the square root of a double.


int i = 256;
int root

root = sqrt( (double) i);
The cast is made by putting the bracketed name of the required type just before the value. (double) in this example.