C nan() Function

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

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.

C
nan("");
CallWhat 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
C
double nan(const char *tagp);
float nanf(const char *tagp);
long double nanl(const char *tagp);

Parameters

ParameterDescription
tagpRequired. A C string used as an implementation-defined NaN tag. Use "" for a plain quiet NaN.

Return Value

Return ValueDescription
double NaNA quiet Not-a-Number value. Comparisons with == are always false.

C Examples

Example 1 — Create and Detect NaN

Build a NaN and confirm it with isnan.

C
#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

  1. Call nan. An empty tag builds a quiet NaN.
  2. Store in double. The bit pattern represents Not a Number.
  3. Test with isnan. That is the clear way to detect it.
  4. Do not use ==. NaN == NaN is false.

Example 2 — Printing a NaN

See how printf shows a NaN value.

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

int main(void) {
    double x = nan("tag");
    printf("value = %f\n", x);
    return 0;
}

How It Works

  1. Pass a tag. Some libraries keep payload bits from the string.
  2. Print with %f. Typical output text is nan.
  3. Still a double. It occupies the same size as other doubles.
  4. 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.

C
#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

  1. Start with NaN. nan("") creates it.
  2. Add a number. The sum stays NaN.
  3. Check the result. isnan(y) is true (prints 1).
  4. Debug tip. Once NaN appears, later steps can stay poisoned.

Frequently Asked Questions

nan() builds a quiet NaN double. You can pass a tag string for an implementation-defined payload.
tagp is required. It is a const char* tag; "" is a common choice.
A double NaN value.
Include <math.h>. On GCC, link with -lm.
Use isnan(x). Do not rely on x == x alone if you want clear intent.

Next: nearbyint()

Round a floating-point value without raising FE_INEXACT.

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