C fmin() Function

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

Definition

In C, fmin() returns the smaller of two double values. fmin(3.5, 1.2) is 1.2. It is the usual floating-point “min” from <math.h> (C99 and later).

If one argument is NaN and the other is a number, fmin() typically returns the number. Both arguments are required. For an int minimum you would write your own compare — fmin is for floating types.

Syntax

Pass two floating-point numbers. Start with this call.

C
fmin(3.5, 1.2);
CallWhat it does
fmin(3.5, 1.2)Returns 1.2, the smaller value.

Include <math.h>. On GCC, link with -lm. Pair it with fmax() for the larger value. Newer C23 names like fminimum() differ mainly in NaN rules.

Library declarations
C
double fmin(double x, double y);
float fminf(float x, float y);
long double fminl(long double x, long double y);

Parameters

ParameterDescription
xRequired. First double.
yRequired. Second double.

Return Value

Return ValueDescription
doubleThe smaller of the two values. If one is NaN, the other number is preferred.

C Examples

Example 1 — Smaller of Two Values

Pick the lower reading from a pair of doubles.

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

int main(void) {
    printf("%.1f\n", fmin(3.5, 1.2));
    printf("%.1f\n", fmin(-2.0, -7.0));
    return 0;
}

How It Works

  1. Include math.h. Declares fmin.
  2. Positive pair. 1.2 is smaller than 3.5.
  3. Negative pair. -7 is smaller than -2.
  4. Return type. Result stays double.

Example 2 — Cap a Value from Above

Use min to force a value not to exceed a maximum.

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

int main(void) {
    double speed = 120.0;
    double limit = 100.0;
    double capped = fmin(speed, limit);

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

How It Works

  1. Speed is high. 120 is above the limit.
  2. Take fmin. The smaller value is 100.
  3. Already under. If speed were 80, fmin would keep 80.
  4. Common pattern. Same idea as min(value, maxAllowed).

Example 3 — Prefer the Real Number

When one side is NaN, fmin usually keeps the numeric side.

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

int main(void) {
    printf("%.1f\n", fmin(NAN, 4.0));
    printf("%.1f\n", fmin(2.5, NAN));
    return 0;
}

How It Works

  1. NaN vs 4. Result is 4.0.
  2. 2.5 vs NaN. Result is 2.5.
  3. Both NaN. Then the result is NaN.
  4. Vs fminimum. fminimum() (C23) returns NaN if either argument is NaN.

Frequently Asked Questions

fmin() returns the smaller of two floating-point values. fmin(3.5, 1.2) is 1.2.
Both x and y are required doubles (or float / long double variants).
The smaller double. If one is NaN, the other number is preferred.
Include <math.h>. On GCC, link with -lm.
fmin() prefers a number over NaN. fminimum() (C23) returns NaN if either argument is NaN.

Next: fminimum_mag_num()

C23 minimum that returns NaN if either argument is NaN.

fminimum_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