C fmax() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, fmax() returns the larger of two double values. fmax(3.0, 7.0) is 7.0.

You pass two double values. The result is a double. Include <math.h>. On GCC, link with -lm.

Syntax

Pass both values. Start with this call.

C
fmax(3.0, 7.0);
CallWhat it does
fmax(3.0, 7.0)Returns 7.0, the larger value.
fmax(-2.0, -5.0)Returns -2.0, which is larger than -5.0.

Include <math.h>. On GCC, link with -lm. For the smaller value, use fmin().

Library declarations
C
double fmax(double x, double y);
float fmaxf(float x, float y);
long double fmaxl(long double x, long double y);

Parameters

ParameterDescription
xRequired. First value to compare.
yRequired. Second value to compare.

Return Value

Return ValueDescription
doubleThe larger of x and y. If one argument is NaN, the other numeric value is typically returned.

C Examples

Example 1 — Basic Maximum

Compare positive and negative pairs.

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

int main(void) {
    printf("%.1f\n", fmax(3.0, 7.0));
    printf("%.1f\n", fmax(-2.0, -5.0));
    return 0;
}

How It Works

  1. Include math.h. That declares fmax.
  2. Positive pair. 7 is larger than 3.
  3. Negative pair. −2 is larger than −5.
  4. Result type. Still a double.

Example 2 — Raise a Value to a Floor

Use fmax to keep a reading from going below a minimum.

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

int main(void) {
    double reading = -1.5;
    double min_ok = 0.0;
    double v = fmax(reading, min_ok);

    printf("v = %.1f\n", v);
    return 0;
}

How It Works

  1. Start below the floor. reading is −1.5.
  2. Set the floor. min_ok is 0.
  3. Call fmax. Result becomes 0.
  4. Clamp pattern. Pair with fmin for a high cap.

Example 3 — One NaN Argument

When one side is NaN, fmax prefers the numeric value.

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

int main(void) {
    double n = nan("");
    printf("%.1f\n", fmax(n, 4.0));
    printf("%.1f\n", fmax(4.0, n));
    return 0;
}

How It Works

  1. Build a NaN. nan("") makes a quiet NaN.
  2. NaN first. Numeric 4.0 is returned.
  3. NaN second. Same numeric result.
  4. Different from fmaximum. C23 fmaximum propagates NaN instead.

Frequently Asked Questions

fmax() returns the larger of two double values. fmax(3, 7) is 7.
x and y are required doubles to compare.
The larger double. If one argument is NaN, the other number is typically returned.
Include <math.h>. On GCC, link with -lm.
fmaximum() is a C23 API with different NaN rules. fmax() is the older math.h maximum.

Next: fmaximum_mag_num()

C23 maximum of two floating-point values.

fmaximum_mag_num() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful