Home C Math Functions nan() C nan() Function Definition In C, nan() builds a quiet NaN — “Not a Number” — as a double. You can pass a short tag string that some libraries store in the NaN payload for debugging.
You pass a const char * tag (often ""). The result is a double NaN. Include <math.h>. On GCC, link with -lm. To test for NaN later, use isnan() .
NaN values appear when a calculation has no real answer, such as 0.0 / 0.0 or sqrt(-1.0). nan() lets you create one on purpose instead of waiting for a bad expression.
Syntax Pass a tag string. An empty string is fine for a plain quiet NaN.
Call What it does nan("")Returns a quiet NaN double. The empty tag is a common choice.
Include <math.h>. On GCC, link with -lm. Printing a NaN usually shows nan or similar text.
Library declarations double nan(const char *tagp);
float nanf(const char *tagp);
long double nanl(const char *tagp); Parameters Parameter Description tagp Required. A C string used as an implementation-defined NaN tag. Use "" for a plain quiet NaN.
Return Value Return Value Description double NaN A quiet Not-a-Number value. Comparisons with == are always false.
C Examples 1. Basic nan 2. Print NaN 3. Propagation Example 1 — Create and Detect NaN Build a NaN and confirm it with isnan.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = nan("");
if (isnan(x)) {
printf("x is NaN\n");
}
return 0;
} How It Works Call nan. An empty tag builds a quiet NaN.Store in double. The bit pattern represents Not a Number.Test with isnan. That is the clear way to detect it.Do not use ==. NaN == NaN is false.Example 2 — Printing a NaN See how printf shows a NaN value.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = nan("tag");
printf("value = %f\n", x);
return 0;
} How It Works Pass a tag. Some libraries keep payload bits from the string.Print with %f. Typical output text is nan.Still a double. It occupies the same size as other doubles.Useful sentinel. Return NaN from a function to mean “no answer”.Example 3 — NaN Propagation Show that math with a NaN input often yields NaN.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = nan("");
double y = x + 5.0;
printf("isnan(y) = %d\n", isnan(y));
return 0;
} How It Works Start with NaN. nan("") creates it.Add a number. The sum stays NaN.Check the result. isnan(y) is true (prints 1).Debug tip. Once NaN appears, later steps can stay poisoned.Frequently Asked Questions What does nan() do in C? nan() builds a quiet NaN double. You can pass a tag string for an implementation-defined payload.
What is the parameter of nan()? tagp is required. It is a const char* tag; "" is a common choice.
What does nan() return? A double NaN value.
Which header file does nan() need? Include <math.h>. On GCC, link with -lm.
How do I test for NaN? Use isnan(x). Do not rely on x == x alone if you want clear intent.
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