What is nextafter() in C?

The nextafter() function is used to produce the smallest possible value after a number in a given direction. The declaration of nextafter() is shown below:

double nextafter (double num, double dir);

Or:

long double nextafterl (long double num, long double dir);

Or:

float nextafterf (float num, float dir);

The nextafter() function returns the next representable floating-point number after num in the direction of dir.

Library

To use the nextafter() function, include the following library:

#include <math.h>

Code

Consider the code snippet below, which demonstrates the implementation of nextafter():

#include <stdio.h>
#include <math.h>
int main() {
double a = 0.0;
double b = 1.0;
double x = nextafter(a, b);
printf("nextafter ( %f, %f ) = ( %e ) \n", a, b, x);
return 0;
}

Explanation

The nextafter() function is used in line 9 to compute the next representable floating-point number after a in the direction of b.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved