Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) operator%

 

Operator to obtain the remainder from an integer division.

 

The following line of code calls operator+ to add the values of two integers.

 

const int three = 7 % 4; //3 is the remainder of 7 divided by 4

 

 

operator% can be often found in prime determination (in the tool PrimeExpert for example).

 

The code below shows how to determine if a value is odd or even.

 

 

if (x%2==0)

{

//x must be even

}

else

{

//x must be odd

}

 

 

The STL functor encapsulating operator% is called std::modulus.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.