O'Reilly logo

Essential C++ by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

1.3. Writing Expressions

The built-in data types are supported by a collection of arithmetic, relational, logical, and compound assignment operators. The arithmetic operators are unsurprising except for integer division and the remainder operator:

// Arithmetic Operators 
+    addition       a + b 
-    subtraction    a - b 
*    multiplication a * b 
/    division       a / b 
%    remainder      a % b 

The division of two integer values yields a whole number. Any remainder is truncated; there is no rounding. The remainder is accessed using the % operator:

5 / 3 evaluates to 1 while 5 % 3 evaluates to 2 
5 / 4 evaluates to 1 while 5 % 4 evaluates to 1 
5 / 5 evaluates to 1 while 5 % 5 evaluates to 0 

When might we actually use the remainder operator? Imagine that we want to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required