Home »
C programming language
Modulus on negative numbers in C language
Last Updated : December 23, 2025
In C language, the modulus operator % behaves differently with negative numbers compared to pure mathematical definitions. When one or both operands are negative, the result depends on how C defines integer division, which truncates toward zero. Understanding this behavior is important to avoid logical errors while working with negative values.
The modulus operator (%) operator in C
The modulus operator
% is an arithmetic and binary operator in the C language that works with two integer operands.
It is primarily used to determine the remainder obtained after performing integer division, which is a common requirement in
loop control, number validation, parity checks, and low-level calculations.
When dealing with negative numbers, the behavior of the modulus operator in C follows the language standard, where integer division
is truncated toward zero. As a result, the sign of the remainder depends on the sign of the dividend
(operand1), not the divisor. This behavior may differ from mathematical modulus rules used in some other languages.
Syntax
operand1 % operand2;
The operator returns the remainder after dividing
operand1 by
operand2.
Both operands must be of integer type, as the modulus operator is not defined for floating-point values in C.
Understanding how the modulus operator works with negative values is essential for writing accurate and predictable C programs, especially in system programming, algorithm design, and competitive coding scenarios.
C program demonstrating the modulus operator with positive operands
Here is a simple example that shows how the % operator works when both operands are positive, returning the remainder after integer division.
// C program to demonstrate example of
// Modules operator (%)
#include <stdio.h>
int main()
{
int a = 10;
int b = 3;
int result;
result = a%b;
printf("result = %d\n", result);
return 0;
}
Output
result = 1
Modulus operator with negative numbers in C
When working with negative numbers in C, the result of the modulus operator
% depends on the sign of the left operand (dividend).
C follows the rule that integer division is truncated toward zero, and therefore the remainder always carries the sign of the left operand.
In simple terms, the sign of the remainder is the same as the sign of the left operand, regardless of whether the right operand is positive or negative.
Understand this behavior using the table below:
Left Operand Right Operand Result (Remainder)
Positive Positive Positive
Positive Negative Positive
Negative Positive Negative
Negative Negative Negative
Example
Input:
int a = -10;
int b = 3;
// finding remainder
result = a % b;
printf("result = %d\n", result);
Output:
result = -1
C program demonstrating the modulus operator with negative operands
The following example illustrates how the % operator behaves when one or both operands are negative in C.
// C program to demonstrate example of
// Modules operator (%)
#include <stdio.h>
int main()
{
int a = -10;
int b = 3;
int result;
result = a%b;
printf("result = %d\n", result);
a = 10;
b = -3;
result = a%b;
printf("result = %d\n", result);
a = -10;
b = -3;
result = a%b;
printf("result = %d\n", result);
return 0;
}
Output
result = -1
result = 1
result = -1
Advertisement
Advertisement