Easy Tutorial
❮ C For Loop C Exercise Example8 ❯

C Library Function - modf()

C Standard Library - <math.h>

Description

The C library function double modf(double x, double *integer) returns the fractional part (the part after the decimal point) and sets integer to the integral part.

Declaration

Here is the declaration for the modf() function.

double modf(double x, double *integer)

Parameters

Return Value

The function returns the fractional part of x, with the same sign as x.

Example

The following example demonstrates the use of the modf() function.

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

int main ()
{
   double x, fractpart, intpart;

   x = 8.123456;
   fractpart = modf(x, &intpart);

   printf("Integral part = %lf\n", intpart);
   printf("Fractional part = %lf \n", fractpart);

   return(0);
}

Let's compile and run the above program, which will produce the following result:

Integral part = 8.000000
Fractional part = 0.123456

C Standard Library - <math.h>

❮ C For Loop C Exercise Example8 ❯