We have seen the operators + (plus) - (minus) * (multiply) and / (divide). We have also observed that they seem to behave sensibly in that * and / get performed before + and -. We are also control the order in which the expressions are evaluated by using brackets. However, there are a few other wrinkles to operators.
The C compiler stays up at night worrying about the speed of the programs it is compiling. In particular it will try to do things to your program which will make it run as fast as possible. One of the things it does is concerned with the way it will compile the divide operator.
If the compiler thinks that you are doing an integer division (i.e. you are dividing one integer by another) it will not bother to work out a floating point answer. Instead it will work out the divisor and throw away any remainder which may be left. It also doesn't bother to round up. This can lead to calculations which don't give the "right" answer as far as maths is concerned:
i = 8 / 3 ;
If I was working out the above sum on my calculator I would get something like 2.66666, which I could round up to 3. C would not do this, and instead would give the result integer 2. If we were using a compiler which could use the floating point type (which BoostC will not) you could put:
x = 8.0 / 3.0 ;
The compiler would go "Aha!, 8.0 is a floating point value so I must use the floating point version of divide and produce a floating point result". This would mean that x (which had better be a floating point variable) would be set to the correct result of 2.6666666).
In C for the PICmicro microcontroller you will have to get used to the fact that every division is integer, which means that rounding never takes place. If you want to round up you will have to do something cunning to force it to happen.
A useful operator which C also provides is % which is sometimes called the modulus operator (I call it remainder). This is the way that you can get hold of the remainder from a division. What % does is perform the division, find out the remainder and then supply that value:
k = 10 % 6 ;
If we are dividing 10 by 6 we could give the answer 1 with remainder 4. This means that k
would be set to 4. I use modulus a lot when I am printing out a number. If I want to print the value 45 I have to print the character 4 followed by character 5. Look at the following code and be amazed!
val = 45;
tens = '0' + (val / 10) ;
units = '0' + (val % 10) ;
This cunning code contraption can print out any value from 00 to 99. The tens are set to the proper ASCII character by adding the division of our value by 10 to the ASCII code of '0' (see above). We then use the remainder operator to get the units digit, which is then added to the ASCII code to give us the character ready for printing.
Note that in this example the fact that numbers are not rounded up works in my favour, otherwise we could have ended up printing 55.
See the program on the right for another example. Note that no rounding up is performed, and that when modulus is used we get the remainder of the division.
/* Divide */
void main ( void )
{
int top = 8 ;
int bottom = 3 ;
/* integer division */
top = top / bottom ;
integer modulus */
top = 50 % 7 ;