HP C A.06.05 Reference Manual
Expressions and Operators
Arithmetic Operators (+, -, *, /, %)
Chapter 584
Arithmetic Operators (+, -, *, /, %)
Syntax
exp1
+
exp2
Adds
exp1
and
exp2
.An
exp
can be any integer or floating-point expression.
exp1
-
exp2
Subtracts
exp2
from
exp1
.
exp1
*
exp2
Multiplies
exp1
by
exp2
.
exp1
/
exp2
Divides
exp1
by
exp2
.
exp1
%
exp2
Finds modulo of
exp1
divided by
exp2
. (That is, finds the remainder of an
integer division.) An expression can be any integer expression.
-
exp
Negates the value of
exp
.
+
exp
Identity (unary plus).
Arguments
exp
Any constant or variable expression.
Description
The addition, subtraction, and multiplication (+, -, and *) operators perform the usual
arithmetic operations in C programs. The operands may be any integral or floating-point
value, with the following exception:
• The modulo operator (%) accepts only integer operands.
• The unary plus operator (+
exp
) and the addition and subtraction operators accept integer,
floating-point, or pointer operands.
The addition and subtraction operators also accept pointer types as operands. Pointer
arithmetic is described in “Pointer Operators (*, ->, &)” on page 118.
C's modulo operator (%) produces the remainder of integer division, which equals 0 if the right
operand divides the left operand exactly. This operator can be useful for tasks such as
determining whether or not a year is an Olympic Games year. For example:
if (year % 4 == 0)
printf("This is an Olympic Games year.\n");
else
printf("There will be no Olympic Games this year.\n");