w3resource

C fmod() function

C fmod() function - Calculate Floating-Point remainder

Syntax:

double fmod(double x, double y)

The fmod() function is used to calculate the floating-point remainder of x/y. The absolute value of the result is always less than the absolute value of y and the result will have the same sign as x.

Parameters:

Name Description Required /Optional
x Floating-point value. Required
y Floating-point value. Required

Return value from fmod()

  • Returns the floating-point remainder of x/y.
  • If y is zero or if x/y causes an overflow, fmod() returns 0.

Example: fmod() function

The following example shows the usage of fmod() function.


#include <math.h>
#include <stdio.h>
 
int main(void)
{
   double x, y, result;
   x = 200.0;
   y = 7.0;
   printf("Before applying fmod()");
   printf("\nx = %lf", x);
   printf("\ny = %lf", y);    
   result = fmod(x,y);
   printf("\n\nAfter applying fmod()");
   printf("\nResult = %lf", result);
   x = -45.0;
   y = 4.0;
   printf("\n\nBefore applying fmod()");
   printf("\nx = %lf", x);
   printf("\ny = %lf", y);    
   result = fmod(x,y);
   printf("\n\nAfter applying fmod()");
   printf("\nResult = %lf", result);
   
}

Output:

Before applying fmod()
x = 200.000000
y = 7.000000

After applying fmod()
Result = 4.000000

Before applying fmod()
x = -45.000000
y = 4.000000

After applying fmod()
Result = -1.000000

C Programming Code Editor:

Previous C Programming: C floor()
Next C Programming: C math.h Home



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
{
    ptr = 0;// oops, I meant *ptr = 0
}

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q