HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 5 Expressions and Operators

Arithmetic Operators (+, -, *, /, %)

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

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 (*, ->, &)”.

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 a U.S. presidential election 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");

As required by the ANSI/ISO C standard, HP C supports the following relationship between the remainder and division operators:

a equals a%b + (a/b) * b for any integer values of a and b

The result of a division or modulo division is undefined if the right operand is 0.

The sign reversal or unary minus operator (-) multiplies its sole operand by -1. For example, if x is an integer with the value -8, then -x evaluates to 8.

The result of the identity or unary plus operator (+) is simply the value of the operand.

Refer to “Operator Precedence ” for information about how these and other operators evaluate with respect to each other.

© Hewlett-Packard Development Company, L.P.