Binary Arithmetic Operators
Division of two integers returns an integer, while remainder is simply truncated:
/* for example: */
7 / 4;
// equals 1
7 * 3 / 4;
// equals 5
/* but: */
7. * 3./ 4.;
// equals 5.25 as we are working with floats
Remainder operand
%
works only with integers; sign of result is equal to the sign
of first operand:
/* for example: */
9 % 3;
// equals 0
7 % 3;
// equals 1
-7 % 3;
// equals -1
We can use arithmetic operators for manipulating characters:
'A' + 32;
// equals 'a' (ASCII only)
'G' - 'A' + 'a';
// equals 'g' (both ASCII and EBCDIC)
Unary Arithmetic Operators
Unary operators
++
and
--
are the only operators in C which can be either prefix
(e.g.
++k
,
--k
) or postfix (e.g.
k++
,
k--
).
When used as prefix, operators
++
and
--
(preincrement and predecrement) add or
subtract one from the value of operand
before
the evaluation. When used as suffix,
operators
++
and
--
add or subtract one from the value of operand
after
the evalu-
ation.
For example:
int
j = 5; j = ++k;
/* k = k + 1, j = k, which gives us j = 6, k = 6 */
int
j = 5; j = k++;
/* j = k, k = k + 1, which gives us j = 5, k = 6 */
MikroElektronika: Development tools - Books - Compilers
103
page
mikroC - C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...