C fpclassify() Macro

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

Definition

In C, fpclassify() is a classification helper from <math.h>. You pass one floating-point value. It returns a category code that says what kind of number you have — for example a normal number, zero, infinity, NaN, or a tiny subnormal value.

Think of it as asking: “What is this float?” instead of doing many separate tests. The common category macros are FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, and FP_NORMAL. It is usually implemented as a macro (or macro-like function) that works with float, double, or long double.

Syntax

Pass one floating-point value. Start with this call.

C
fpclassify(3.14);
CallWhat it does
fpclassify(3.14)Returns FP_NORMAL for an ordinary finite non-zero value.
fpclassify(0.0)Returns FP_ZERO.
fpclassify(INFINITY)Returns FP_INFINITE.

Include <math.h>. On GCC, link with -lm when you also use other math functions. Compare the result with FP_* macros in a switch or if.

Library declarations
C
int fpclassify(real-floating x);

Parameters

ParameterDescription
xRequired. Any real floating type (float, double, or long double).

Return Value

Return ValueDescription
FP_NANx is Not-a-Number.
FP_INFINITEx is +infinity or -infinity.
FP_ZEROx is +0 or -0.
FP_SUBNORMALx is a tiny non-zero value below the normal range.
FP_NORMALx is a normal finite non-zero number.

C Examples

Example 1 — Label Ordinary Values

Print a short name for each category.

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

static const char *kind(double x) {
    switch (fpclassify(x)) {
        case FP_NAN:       return "nan";
        case FP_INFINITE:  return "inf";
        case FP_ZERO:      return "zero";
        case FP_SUBNORMAL: return "subnormal";
        case FP_NORMAL:    return "normal";
        default:           return "other";
    }
}

int main(void) {
    printf("%s\n", kind(3.14));
    printf("%s\n", kind(0.0));
    printf("%s\n", kind(INFINITY));
    return 0;
}

How It Works

  1. Include math.h. Brings in fpclassify and the FP_* macros.
  2. Switch on result. Map each category to a short string.
  3. Normal number. 3.14 is FP_NORMAL.
  4. Zero and infinity. 0.0 is FP_ZERO; INFINITY is FP_INFINITE.

Example 2 — Catch NaN with FP_NAN

Invalid floating results often become NaN.

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

int main(void) {
    double bad = 0.0 / 0.0;

    if (fpclassify(bad) == FP_NAN) {
        printf("got NaN\n");
    } else {
        printf("not NaN\n");
    }
    return 0;
}

How It Works

  1. Make a NaN. 0.0 / 0.0 is undefined and yields NaN.
  2. Classify. fpclassify(bad) returns FP_NAN.
  3. Branch. Handle the error path before more math.
  4. Related helpers. isnan() is a shorter yes/no check for the same idea.

Example 3 — Guard Before Using a Value

Only compute further when the value is a normal number.

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

int main(void) {
    double x = 4.0;

    if (fpclassify(x) == FP_NORMAL) {
        printf("sqrt = %.1f\n", sqrt(x));
    } else {
        printf("skip\n");
    }
    return 0;
}

How It Works

  1. Check first. Confirm x is FP_NORMAL.
  2. Then compute. Call sqrt only on a safe value.
  3. Other cases. Zero, NaN, or infinity can take a different path.
  4. Why one call. fpclassify covers many cases in a single test.

Frequently Asked Questions

It returns a category for a floating-point value such as FP_NORMAL, FP_ZERO, FP_INFINITE, FP_NAN, or FP_SUBNORMAL.
x is required. It can be float, double, or long double.
An int category code. Compare it with the FP_* macros from math.h.
Include <math.h>.
It is provided as a classification macro/function in math.h. Use it like a function call; the library may implement it as a macro.

Next: frexp()

Split a floating-point value into fraction and power of two.

frexp() 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