Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

nullptr — null pointer constant

In C23, nullptr denotes a null pointer constant with its own type, distinct from pointer and arithmetic types.

Version: C23; repository compiler flag, production runtime not attested

Syntax

nullptr
int *p = nullptr;

nullptr is a C23 keyword denoting a predefined null pointer constant. It needs no header. Its type, named nullptr_t in <stddef.h>, is neither a pointer nor an arithmetic type.

Use nullptr for pointer initialization, assignment, and equality comparisons. Conversion to a pointer type produces a null pointer; conversion to bool produces false. Do not perform arithmetic on it or dereference a null pointer.

The integer literal 0 has type int, whereas nullptr always has type nullptr_t. In C23, the NULL macro expands to an implementation-defined null pointer constant. It may expand to nullptr, in which case the expansion has exactly the same type: nullptr_t. The example tests equality, not the pointer's address or representation.

Examples

#include <stdio.h>

int main(void)
{
    int *p = nullptr;
    printf("%d\n", p == nullptr);
    return 0;
}

Result

1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources