C isnan() Macro

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

Definition

In C, isnan() is a <math.h> classification macro. It tells you if a floating value is NaN (Not a Number). True is non-zero; false is 0.

NaN appears after invalid math such as 0.0/0.0 or sqrt(-1.0) for real floating types. Comparing with == is a poor NaN test because NaN is never equal to itself.

Pass one real floating argument. Include <math.h>. Print the result as 0 or 1. Related compare macros stay quieter when NaN shows up.

Syntax

Ask whether a value is NaN. Start with this call.

C
isnan(x);
CallWhat it does
isnan(NAN)Returns non-zero because the value is NaN.
isnan(0.0/0.0)Also non-zero. Invalid division creates NaN.
isnan(3.14)Returns 0 because a normal number is not NaN.

Include <math.h>. On GCC, link with -lm. Prefer isnan(x) over x != x when you want a clear, portable NaN test.

Library declarations
C
#define isnan(x) /* implementation-defined macro */

Parameters

ParameterDescription
xRequired. The floating value to classify (float, double, or long double).

Return Value

Return ValueDescription
Non-zero (true)When x is NaN.
0 (false)When x is finite or infinity.

C Examples

Example 1 — Basic NaN Check

Print 1 for NaN and 0 for ordinary values.

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

int main(void) {
    printf("%d\n", (int)isnan(NAN));
    printf("%d\n", (int)isnan(12.5));
    printf("%d\n", (int)isnan(INFINITY));
    return 0;
}

How It Works

  1. Include math.h. That provides isnan, NAN, and INFINITY.
  2. True NaN. The first line prints 1.
  3. Finite number. A normal value prints 0.
  4. Infinity is different. Infinity is not NaN, so the third line is also 0.

Example 2 — Catch NaN from Bad Math

Square root of a negative value creates NaN on many systems.

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

int main(void) {
    double bad = sqrt(-1.0);

    printf("%d\n", (int)isnan(bad));
    return 0;
}

How It Works

  1. Call sqrt on a negative. Real square root cannot return a normal number here.
  2. Result becomes NaN. The library returns a quiet NaN.
  3. Test with isnan. The macro returns true.
  4. Print 1. You have a clear signal that the math failed.

Example 3 — Guard Before You Trust a Result

Reject NaN before you print or store a computed value.

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

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

    if (isnan(x)) {
        printf("invalid\n");
    } else {
        printf("%.2f\n", x);
    }

    return 0;
}

How It Works

  1. Create invalid math. 0.0 / 0.0 produces NaN.
  2. Check first. isnan(x) is true.
  3. Skip printing the number. Show a clear message instead.
  4. Protect callers. This pattern keeps NaN from spreading into later logic.

Frequently Asked Questions

isnan() returns non-zero when its argument is NaN, otherwise 0.
x is required. It is the floating value you want to classify.
A non-zero value for NaN, or 0 for finite numbers and infinities.
Include <math.h>. On GCC, link with -lm.
NaN never compares equal, even to itself. Use isnan(x) instead.

Next: isnormal()

Test whether a floating value is a normal number.

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