liveBook · Manning

Null pointer constant

As explained in Modern C

A null pointer constant is an integer constant expression with value zero or such a value converted to void pointer, as permitted by the C standard.

Consider these declarations: double const*const nix = 0; double const*const nax = nix; The literal 0 is an integer constant expression with value zero. Used where a pointer is expected, it represents a null pointer. A null pointer constant is not a const-qualified pointer object: “constant” means compile-time constant, and the permitted type is integer or void*. section 11.1.5

double const*const nix = 0;
double const*const nax = nix;


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":12}],[{\"line\":0,\"ch\":7},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":12}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":12}],[{\"line\":0,\"ch\":7},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":12}]]"}
!@%STYLE%@!

The declarations distinguish a null pointer value from the pointer objects that contain it. section 11.1.5

The C standard permits NULL to expand to several forms:

  • 0, 0L, or other integer constant expressions with value zero. section 11.1.5
  • (void*)0, which emphasizes that the value is being used as a pointer. section 11.1.5
  • NULL has no prescribed underlying type, so using it in a variadic function can be nonportable; implementations may provide int 0 or void* 0. section 16.5.2

Choosing a spelling

Prefer 0 when a null pointer value is sufficient. If the pointer intent must be explicit, use (void*)0 directly. Avoid NULL: its name suggests a pointer constant, but its implementation-defined expansion can obscure its type and break variadic calls such as printf("%p: %s\n", NULL, "print of NULL"). section 11.1.5 section 16.5.2

How Modern C covers it

How deep it goes
Defines permitted types, distinguishes compile-time constants from const objects, and traces NULL into variadic-call failures.
Examples
Uses const pointer declarations, permitted NULL expansions, and a printf call demonstrating variadic type mismatch.
What it assumes
Assumes basic pointer declarations while defining null pointer constants and compile-time constant terminology explicitly.
What you can do after
Evaluate null-pointer spellings and avoid NULL where its unspecified type can compromise portability.
How it connects
Connects null pointer constants with pointers, NULL, variadic functions, and portability.

Also explained in

Compare how 2 books explain null pointer constant →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.