C++Assignment OperatorsModulus Assignment

%= Assignment Operator in C++

In C++, the %= operator is a compound assignment operator that calculates the remainder of the division of the left-hand operand by the right-hand operand and assigns the result back to the left-hand operand.

It simplifies expressions like a = a % b; into a %= b;, improving code readability. The % operator (modulus) only works with integers and compatible types, so it’s not valid for floating-point or string types.


Syntax

a %= b;  // Equivalent to: a = a % b;

This performs the modulus operation and stores the result in a.


Example with Integers

modulus_integers.cpp
#include <iostream>
int main() {
    int a = 10;
    int b = 3;
    a %= b;
    std::cout << "a = " << a << std::endl;  // Output: 1
    return 0;
}

Explanation:

  • 10 % 3 = 1 (remainder of division)
  • The value 1 is stored in a.

Example with Negative Numbers

modulus_negative.cpp
#include <iostream>
int main() {
    int a = -13;
    int b = 5;
    a %= b;
    std::cout << "a = " << a << std::endl;  // Output: -3
    return 0;
}

Explanation:

  • The result of % with negative operands depends on the implementation.
  • In most compilers, -13 % 5 = -3.

Example with char Type

modulus_char.cpp
#include <iostream>
int main() {
    char ch = 66;  // ASCII 'B'
    ch %= 10;
    std::cout << "ch = " << (int)ch << std::endl;  // Output: 6
    return 0;
}

Explanation:

  • char is treated as an integer.
  • 66 % 10 = 6

Example with Unsigned Integers

modulus_unsigned.cpp
#include <iostream>
int main() {
    unsigned int x = 17;
    unsigned int y = 6;
    x %= y;
    std::cout << "x = " << x << std::endl;  // Output: 5
    return 0;
}

Explanation:

  • Modulus with unsigned int behaves like with int.
  • 17 % 6 = 5

Example with Floating-Point Numbers

modulus_floats_error.cpp
// float a = 10.5f;
// float b = 2.5f;
// a %= b;  // ❌ Error: invalid operands to binary expression

Explanation:

  • The % and %= operators do not work with float or double.
  • Use fmod() from <cmath> if you need floating-point remainders:
modulus_floats_workaround.cpp
#include <iostream>
#include <cmath>
int main() {
    double a = 10.5;
    double b = 2.5;
    a = std::fmod(a, b);
    std::cout << "a = " << a << std::endl;  // Output: 0.5
    return 0;
}

Example with Arrays

Arrays do not support %= directly.

modulus_arrays_invalid.cpp
int a[3] = {9, 12, 15};
int b[3] = {2, 5, 4};
// a %= b;  // ❌ Error: invalid operands

Correct Way: Element-wise modulus

modulus_arrays_loop.cpp
#include <iostream>
int main() {
    int a[3] = {9, 12, 15};
    int b[3] = {2, 5, 4};
    for (int i = 0; i < 3; i++) {
        a[i] %= b[i];
    }
    for (int i = 0; i < 3; i++) {
        std::cout << "a[" << i << "] = " << a[i] << std::endl;
    }
    return 0;
}

Example with Pointers

%= cannot be used on pointers, but it can be used on values pointed to.

modulus_pointer_value.cpp
#include <iostream>
int main() {
    int x = 29;
    int* ptr = &x;
    *ptr %= 4;
    std::cout << "x = " << x << std::endl;  // Output: 1
    return 0;
}

Example with Custom Types

You can overload the %= operator in your own class:

custom_operator_modulus.cpp
#include <iostream>
class Counter {
    int count;
public:
    Counter(int c) : count(c) {}
    Counter& operator%=(int val) {
        count %= val;
        return *this;
    }
    void show() {
        std::cout << "Count = " << count << std::endl;
    }
};
 
int main() {
    Counter c(27);
    c %= 6;
    c.show();  // Output: Count = 3
    return 0;
}

Division by Zero (Modulus)

Just like division, modulus by zero is undefined behavior.

modulus_by_zero.cpp
// int a = 5;
// int b = 0;
// a %= b;  // ❌ Runtime error: division by zero

Solution:

Check the denominator before using %=:

safe_modulus.cpp
if (b != 0) {
    a %= b;
} else {
    std::cout << "Cannot perform modulus by zero!" << std::endl;
}

Summary Table

Data TypeSupportedExampleNotes
intYesa %= bTruncates to remainder
charYesch %= 10Treated as small integer
unsigned intYesx %= yValid; behaves as expected
float/doubleNof %= xNot supported; use fmod() instead
std::stringNos %= xNot supported
ArraysNoarr1 %= arr2Use loop for element-wise operation
PointersIndirect*ptr %= valWorks on dereferenced pointer
Custom ClassYesOverload operator%=Define your own behavior

Conclusion

The %= operator is useful when you need to calculate remainders efficiently. While it’s commonly used with integer values, it’s important to remember it doesn’t work with floating-point or string types. Also, always guard against modulus by zero, which results in undefined behavior.

Understanding and using %= correctly will help you write more expressive and efficient C++ programs.