modf : <math.h> library function

The function double modf(double x, double *intPart); breaks x into an integral and a fractional part. The fractional part is returned by the function and integral part is stored in object pointed by intPart pointer.

Function prototype of modf

double modf(double x, double *intPart);
  • x : A floating point number to break into parts.
  • intPart : A pointer to an object where the integral part is stored.

Return value of modf

It returns the fractional part of x. The fractional and integral part have the same sign as x.

C program to show the use of modf function

The following program shows the use of modf function to break a floating point number into integral and fractional part.

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

int main ()
{
  double x, integer, fraction;
  printf("Enter a floating point number\n");
  scanf("%lf", &x);
  
  fraction = modf(x, &integer);
  printf("Integer part of %lf : %lf\n", x, integer);
  printf("Fractional part of %lf : %lf", x, fraction);
  
  return 0;
}

Program Output
Enter a floating point number
25.2345
Integer part of 25.234500 : 25.000000
Fractional part of 25.234500 : 0.234500