The modulo operator (%) in C calculates the remainder of the division of one integer by another. Here’s how it works:
Syntax:
cCopy code
result = a % b;
a is the dividend.
b is the divisor.
result is the remainder after dividing a by b.
Examples:
10 % 3 results in 1 because 10 divided by 3 gives a quotient of 3 and a remainder of 1.
15 % 4 results in 3 because 15 divided by 4 gives a quotient of 3 and a remainder of 3.
Rules:
The modulo operator works with integers, not floating-point numbers.
If a is negative, the result will also be negative or zero, depending on the remainder. For example:
-10 % 3 results in -1.
Common Uses:
Checking divisibility: If a % b == 0, it means a is divisible by b.
Cycling through values: Often used in looping or circular indexing.
Would you like examples or further clarification?