C fmod() Function

Definition
In C, fmod() returns the remainder of x / y. The sign follows x. fmod(5.5, 2) is 1.5.
You pass two double values, and both are required. y must not be 0. The result is a double. Include <math.h>.
Syntax
Pass the dividend, then the divisor. Start with this call.
| Call | What it does |
|---|
fmod(5.5, 2.0) | Returns 1.5, the remainder of 5.5 divided by 2. |
Include <math.h>. On GCC, link with -lm.
Library declarations
double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);
Parameters
| Parameter | Description |
|---|
| x | Required. The dividend. The remainder keeps this sign. |
| y | Required. The divisor. It must not be 0. |
Return Value
| Return Value | Description |
|---|
| Remainder | A double with the sign of x. |
| NaN | Returned when y is 0. |
C Examples
Example 1 — Basic Remainder
Get what is left after fitting as many whole y chunks as possible into x (toward zero).
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", fmod(10.5, 3.2));
printf("%.1f\n", fmod(9.6, 3.2));
return 0;
}
How It Works
- Include math.h. That declares
fmod. - 10.5 and 3.2. Three full chunks leave
0.9. - Exact multiple.
9.6 = 3 × 3.2, so the remainder is 0.0. - Print as double.
%.1f shows one decimal place.
Example 2 — Sign Follows the Dividend
Changing the sign of x flips the remainder’s sign. Changing only y does not.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("fmod(-10.5, 3.2) = %.1f\n", fmod(-10.5, 3.2));
printf("fmod(10.5, -3.2) = %.1f\n", fmod(10.5, -3.2));
return 0;
}
fmod(-10.5, 3.2) = -0.9
fmod(10.5, -3.2) = 0.9
How It Works
- Negative x. Truncated quotient is
-3; remainder is -0.9. - Negative y. Quotient truncates to
-3 as well; remainder stays +0.9. - Rule. Sign matches the dividend, not the divisor.
- Plan for signs. If you need a non-negative wrap, adjust after
fmod.
Example 3 — Wrap Degrees into [0, 360)
Keep an angle in a single turn. Negative inputs need a small fix-up after fmod.
#include <stdio.h>
#include <math.h>
double wrap360(double deg) {
double r = fmod(deg, 360.0);
if (r < 0.0) {
r += 360.0;
}
return r;
}
int main(void) {
printf("%.1f\n", wrap360(400.0));
printf("%.1f\n", wrap360(-30.0));
return 0;
}
How It Works
- 400° wraps.
fmod(400, 360) is 40. - Negative input.
fmod(-30, 360) is -30 (sign of x). - Shift into range. Adding
360 yields 330. - Useful pattern. Same idea works for radians with
2π.
Frequently Asked Questions
fmod() returns the remainder of x divided by y. fmod(5.5, 2) is 1.5.
Both are required. x is the dividend and y is the divisor.
A double with the sign of x. If y is 0, the result is NaN.
Include <math.h>. On GCC, link with -lm.
No. A zero divisor is a domain error.
Next: frexp()
Split a floating value into a normalized fraction and an exponent.
frexp() tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful