Home C Math Functions isnan() C isnan() Macro 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.
Call What 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 #define isnan(x) /* implementation-defined macro */ Parameters Parameter Description x Required. The floating value to classify (float, double, or long double).
Return Value Return Value Description Non-zero (true) When x is NaN. 0 (false) When x is finite or infinity.
C Examples 1. Basic isnan 2. Make a NaN 3. Guard before print Example 1 — Basic NaN Check Print 1 for NaN and 0 for ordinary values.
#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 Include math.h. That provides isnan, NAN, and INFINITY.True NaN. The first line prints 1.Finite number. A normal value prints 0.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.
#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 Call sqrt on a negative. Real square root cannot return a normal number here.Result becomes NaN. The library returns a quiet NaN.Test with isnan. The macro returns true.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.
#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 Create invalid math. 0.0 / 0.0 produces NaN.Check first. isnan(x) is true.Skip printing the number. Show a clear message instead.Protect callers. This pattern keeps NaN from spreading into later logic.Frequently Asked Questions What does isnan() do in C? isnan() returns non-zero when its argument is NaN, otherwise 0.
What is the parameter of isnan()? x is required. It is the floating value you want to classify.
What does isnan() return? A non-zero value for NaN, or 0 for finite numbers and infinities.
Which header file does isnan() need? Include <math.h>. On GCC, link with -lm.
Why not write x == NAN? NaN never compares equal, even to itself. Use isnan(x) instead.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion