C fmod() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
fmod(5.5, 2.0);
CallWhat 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
C
double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);

Parameters

ParameterDescription
xRequired. The dividend. The remainder keeps this sign.
yRequired. The divisor. It must not be 0.

Return Value

Return ValueDescription
RemainderA double with the sign of x.
NaNReturned 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).

C
#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

  1. Include math.h. That declares fmod.
  2. 10.5 and 3.2. Three full chunks leave 0.9.
  3. Exact multiple. 9.6 = 3 × 3.2, so the remainder is 0.0.
  4. 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.

C
#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;
}

How It Works

  1. Negative x. Truncated quotient is -3; remainder is -0.9.
  2. Negative y. Quotient truncates to -3 as well; remainder stays +0.9.
  3. Rule. Sign matches the dividend, not the divisor.
  4. 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.

C
#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

  1. 400° wraps. fmod(400, 360) is 40.
  2. Negative input. fmod(-30, 360) is -30 (sign of x).
  3. Shift into range. Adding 360 yields 330.
  4. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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