next up previous
Next: Logic Up: September 20 Previous: September 20


Arithmetic

The arithmetic operators you're familiar with from Java were (pretty much) inherited from C: +, -, *, /, %. In C the modulus

If you mix ``narrower'' and ``wider'' types in an arithmetic expression, the result is wider:

int i;
double d;
... i + d <--- double expression
An assignment from a wider to a narrow type is legal. It may generate a warning:
int i= 3;
double d= 3.14159;

... i= d <----- truncation may generate warning
Use a cast statement to document (and reassure the compiler) that you know what you're doing:
int i= 3;
double d= 3.14159;

i= (int) d;



Danny Heap 2002-12-16