- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - modf() function
The C library modf(double x, double *integer) function of type double returns the fraction component (part after the decimal), and sets integer to the integer component.
Syntax
Following is the syntax of the C library function modf() −
double modf(double x, double *integer)
Parameters
This function accepts two 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 1: Handling postive values
Following is the basic C library function modf() to see its demonstration.
#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); }
Output
On execution of above code, we get the following result −
Integral part = 8.000000 Fraction Part = 0.123456
Example 2: Handling negative values
Below the program calculate the integral and fractional parts of the value which is y. Also, we can replace this value with another value i.e. assigned by the users.
#include <stdio.h> #include <math.h> int main() { // Replace with your desired negative value double y = -8.64329; double int_part, frac_part; frac_part = modf(y, &int_part); printf("Original value: %.6lf\n", y); printf("Integer part: %.0lf\n", int_part); printf("Fractional part: %.6lf\n", frac_part); return 0; }
Output
After executing the code, we get the following result −
Original value: -8.643290 Integer part: -8 Fractional part: -0.643290