C modf() Function

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

Definition

In C, modf() splits x into a fraction and a whole-number part. The call returns the fraction and writes the whole part through a pointer. modf(3.25, &whole) returns 0.25 and sets whole to 3.

You pass the double and a pointer to a double. Both are required. Both parts keep the sign of x. Include <math.h>.

Syntax

Pass the value and the address of a double. Start with these lines.

C
double whole;
double frac = modf(3.25, &whole);
CallWhat it does
modf(3.25, &whole)Returns 0.25 and stores 3 in whole.

Include <math.h>. On GCC, link with -lm. The pointer must point to a double you own.

Library declarations
C
double modf(double x, double *iptr);
float modff(float x, float *iptr);
long double modfl(long double x, long double *iptr);

Parameters

ParameterDescription
xRequired. The double to split.
iptrRequired. A pointer to a double. The function writes the whole-number part there.

Return Value

Return ValueDescription
FractionThe part after the decimal point, with the same sign as x.

C Examples

Example 1 — Split a Positive Number

Store the whole part through a pointer; print the returned fraction.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double value = 123.456;
    double ipart;
    double frac = modf(value, &ipart);

    printf("value = %.3f\n", value);
    printf("ipart = %.1f\n", ipart);
    printf("frac  = %.3f\n", frac);

    return 0;
}

How It Works

  1. Declare storage. ipart will receive the whole part.
  2. Pass the address. &ipart is the iptr argument.
  3. Read both results. Return value is the fraction; ipart is updated in place.
  4. Link math. Use -lm on GCC for modf.

Example 2 — Negative Numbers Keep the Sign

Both parts follow the sign of the input. The integer part is not floor.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double value = -3.75;
    double ipart;
    double frac = modf(value, &ipart);

    printf("ipart = %.1f\n", ipart);
    printf("frac  = %.2f\n", frac);
    printf("sum   = %.2f\n", ipart + frac);

    return 0;
}

How It Works

  1. Truncate toward zero. -3.75 → integral -3.0.
  2. Fraction stays negative. Return value is -0.75.
  3. Not floor. floor(-3.75) would be -4.
  4. Add them back. ipart + frac rebuilds the original value.

Example 3 — Separate Dollars and Cents

Split a money amount into whole dollars and a fractional part, then convert the fraction to cents.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double amount = 19.99;
    double dollars;
    double frac = modf(amount, &dollars);
    int cents = (int)(frac * 100.0 + 0.5);

    printf("dollars = %.0f\n", dollars);
    printf("cents   = %d\n", cents);

    return 0;
}

How It Works

  1. Split the amount. modf gives dollars and a fraction.
  2. Scale the fraction. Multiply by 100 for cents.
  3. Round carefully. + 0.5 before the cast reduces float noise for display demos.
  4. Note on money. Real finance often uses integers (cents) end-to-end; this shows the split idea only.

Frequently Asked Questions

modf() splits x into a fraction and a whole-number part. It returns the fraction and writes the whole part through a pointer.
Both are required. x is the double to split. iptr is a pointer to a double that receives the whole part.
The fractional part as a double. For 3.25, the fraction is 0.25 and the stored whole part is 3.
Include <math.h>. On GCC, link with -lm.
Yes. modf(-3.25, &whole) returns -0.25 and stores -3.

Next: pow()

Raise a base to an exponent with the math library.

pow() 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