C Math modf() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The modf() function splits a floating-point number into its integral (whole-number) part and fractional part. The fractional part is returned; the integral part is written through a pointer. Handy for formatting money, parsing decimals, and any task that treats whole and fractional pieces separately.

01

Split

Two parts.

02

math.h

Link -lm.

03

Pointer

Stores int part.

04

Returns frac

|frac| < 1.

05

vs fmod

Not remainder.

06

Sign rules

Same as value.

Definition and Usage

modf(value, &iptr) decomposes value into an integral part stored at *iptr and a fractional part returned by the function, such that:

value = integral + fractional

The fractional part has the same sign as value (or is zero) and its magnitude is less than 1. For 123.456, the integral part is 123.0 and the fractional part is 0.456.

💡
Beginner Tip

Do not confuse modf with fmod. modf splits one number; fmod(x, y) computes the remainder of division. The names look alike but the jobs are different.

📝 Syntax

Standard C declaration:

C
double modf(double value, double *iptr);

Related variants

C
float modff(float value, float *iptr);             /* <math.h> */
long double modfl(long double value, long double *iptr); /* <math.h> */

Parameters

  • value — the floating-point number to decompose.
  • iptr — pointer to double where the integral part is stored (must not be null).

Return Value

  • The fractional part of value as double.
  • Integral part written to *iptr.
  • modf(123.456, &w) returns 0.456 and sets w = 123.0.

Headers and linking

  • #include <math.h>
  • Compile: gcc modf.c -std=c11 -o modf -lm
  • Integral part is truncated toward zero (like trunc), not floored.

⚡ Quick Reference

CallFractional returnIntegral (*iptr)
modf(123.456, &w)0.456123.0
modf(7.0, &w)0.07.0
modf(-3.75, &w)-0.75-3.0
modf(0.0, &w)0.00.0
w + modf(v,&w)Reconstructs v (finite values)
Split
modf(v, &w)

One number → two parts

Remainder
fmod(x, y)

Division leftover

Truncate
trunc(v)

Integral only

Identity
v = w + frac

Parts sum to whole

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. modf is the standard way to peel off the decimal portion of a floating-point value in C.

📚 Getting Started

Split a positive floating-point number into integral and fractional parts.

Example 1 — Split 123.456

Classic example from the reference: decompose 123.456.

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

int main(void) {
    double value = 123.456;
    double integralPart;
    double fractionalPart = modf(value, &integralPart);

    printf("Original value:  %.6f\n", value);
    printf("Integral part:   %.6f\n", integralPart);
    printf("Fractional part: %.6f\n", fractionalPart);

    return 0;
}

How It Works

modf returns the part after the decimal point and stores the whole-number part through the pointer. Both add back to the original.

Example 2 — Negative Number: −3.75

Both parts keep the sign of the original value; integral part truncates toward zero.

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

int main(void) {
    double value = -3.75;
    double integral;
    double fractional = modf(value, &integral);

    printf("value = %.2f\n", value);
    printf("integral   = %.2f  (trunc toward zero)\n", integral);
    printf("fractional = %.2f  (same sign as value)\n", fractional);
    printf("sum        = %.2f\n", integral + fractional);

    return 0;
}

How It Works

floor(-3.75) would be −4, but modf integral part is −3 (truncation). The fractional part is −0.75, not +0.25.

📈 Practical Patterns

Reconstruction, money formatting, and comparison with fmod.

Example 3 — Reconstruct the Original Value

Verify integral + fractional == value for several inputs.

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

int main(void) {
    double values[] = { 42.0, 3.14159, -99.99, 0.0 };
    size_t i;

    printf("  value      integral  fractional   sum\n");
    for (i = 0; i < 4; i++) {
        double v = values[i];
        double integral;
        double frac = modf(v, &integral);
        printf("%9.5f  %9.2f  %10.5f  %9.5f\n",
               v, integral, frac, integral + frac);
    }

    return 0;
}

How It Works

The split is exact in principle; tiny rounding differences may appear only at extreme precision. For typical double values the sum matches the input.

Example 4 — Extract Dollars and Cents

Use the integral part for whole dollars and scale the fractional part for cents display.

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

int main(void) {
    double price = 19.99;
    double dollars;
    double frac = modf(price, &dollars);
    int cents = (int)round(frac * 100.0);

    printf("Price: $%.2f\n", price);
    printf("Dollars: $%.0f\n", dollars);
    printf("Cents:   %d\n", cents);

    return 0;
}

How It Works

For production money code, prefer integer cents (1999) to avoid float rounding traps. modf still illustrates the split clearly for learners.

Example 5 — modf() vs fmod()

Same-sounding names, different jobs: split vs remainder.

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

int main(void) {
    double value = 10.5;
    double integral;

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

    printf("modf: splits into parts\n");
    printf("  fractional = %.1f\n", modf(value, &integral));
    printf("  integral   = %.1f\n\n", integral);

    printf("fmod: remainder of division\n");
    printf("  fmod(%.1f, 3.2) = %.1f\n", value, fmod(value, 3.2));

    return 0;
}

How It Works

modf(10.5) gives 10 and 0.5. fmod(10.5, 3.2) asks how much is left after dividing by 3.2—completely unrelated to splitting decimals.

🚀 Common Use Cases

  • Number formatting — separate whole and decimal portions for display.
  • Financial UI — dollars vs cents (prefer integer cents in production).
  • Graphics — split texture coordinates into tile index and offset.
  • Time parsing — whole hours plus fractional hour remainder.
  • Custom rounding — process integral and fractional parts with different rules.

🧠 How modf() Works

1

Receive value

Any finite double (or float/long double variant).

Input
2

Truncate toward zero

Integral part stored at *iptr (like trunc).

Split
3

Return fractional part

value - integral, same sign, |frac| < 1.

Output
=

Two parts

integral + fractional == value.

📝 Notes

  • Fractional part has the same sign as value (or is zero).
  • Integral part is truncated toward zero, not floored.
  • iptr must point to valid writable memory (not NULL).
  • NaN input → NaN returned; integral part unspecified.
  • ±∞ input → returns ±0; integral part is ±∞.
  • Not the same as fmod() or integer %.
  • Link with -lm on GCC/Clang Unix-like builds.

⚡ Optimization

modf() is implemented in the platform math library and is typically fast. For very hot paths you might cast to integer types when you know the range fits—but modf handles negatives, large magnitudes, and IEEE edge cases correctly. Profile before replacing it.

Conclusion

modf() splits a floating-point number into integral and fractional parts in one call. The fractional part is returned; the integral part is stored through a pointer. Use it for formatting and decomposition—not for division remainders (fmod).

Continue with pow() for exponentiation, or review trunc() when you only need the whole-number part.

💡 Best Practices

✅ Do

  • Pass a valid pointer for the integral part
  • Use modf to split one float into two parts
  • Remember negative sign rules on both parts
  • Match type with modff / modfl
  • Include <math.h> and link -lm

❌ Don’t

  • Confuse modf with fmod
  • Pass NULL as the integral pointer
  • Assume fractional part is always positive for negatives
  • Use float dollars for real money without integer cents
  • Expect floor behavior on the integral part

Key Takeaways

Knowledge Unlocked

Five things to remember about modf()

Split floats into parts in C, explained simply.

5
Core concepts
📚 02

Pointer

Stores int.

API
📈 03

|frac| < 1

Decimal bit.

Rule
📄 04

Sign

Both match.

Negative
🔄 05

vs fmod

Different.

Compare

❓ Frequently Asked Questions

modf(value, &iptr) splits a floating-point number into two parts: it returns the fractional part and stores the integral (whole-number) part at the address pointed to by iptr. For example, modf(123.456, &whole) returns 0.456 and whole becomes 123.0.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double modf(double value, double *iptr).
modf splits one number into integer and fractional parts. fmod(x, y) returns the remainder of dividing x by y. They solve completely different problems despite similar names.
Both parts keep the sign of the original value. modf(-3.75, &iptr) returns -0.75 and stores -3.0 in iptr. The integral part is truncated toward zero, not floored.
Yes: value == iptr + modf(value, &iptr) for finite values (within floating-point rounding). That identity is the definition of how modf splits the number.
modff(value, &iptr) uses float types. modfl(value, &iptr) uses long double types. Use the variant matching your variable type.
Did you know?

The name modf comes from “modulus fraction”—the fractional part after removing the integer magnitude. It is one of the oldest math.h utilities, paired in design with functions like frexp that also decompose numbers for low-level numeric work.

Explore C Math Functions

Continue with pow() or browse the full math function reference.

Math Functions Index →

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.

5 people found this page helpful