C library function - modf()

Advertisements


Description

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

Declaration

Following is the declaration for modf() function.

double modf(double x, double *integer)

Parameters

  • x -- This is the floating point value.

  • integer -- This is the pointer to an object where the integral part is to be stored.

Return Value

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

Example

The following example shows the usage of 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("Fraction Part = %lf \n", fractpart);
   
   return(0);
}

Let us compile and run the above program, this will produce the following result:

Integral part = 8.000000
Fraction Part = 0.123456 


Advertisements
Advertisements