### Question 5.4

What is `NULL` and how is it `#define`d?

As a matter of style, many programmers prefer not to have unadorned `0`'s scattered through their programs. Therefore, the preprocessor macro `NULL` is `#define`d (by `<stdio.h>` or `<stddef.h>`) with the value `0`, possibly cast to `(void *)` (see also question 5.6). A programmer who wishes to make explicit the distinction between `0` the integer and `0` the null pointer constant can then use `NULL` whenever a null pointer is required.

Using `NULL` is a stylistic convention only; the preprocessor turns `NULL` back into `0` which is then recognized by the compiler, in pointer contexts, as before. In particular, a cast may still be necessary before `NULL` (as before `0`) in a function call argument. The table under question 5.2 above applies for `NULL` as well as `0` (an unadorned `NULL` is equivalent to an unadorned 0).

`NULL` should only be used for pointers; see question 5.9.

References: K&R1 Sec. 5.4 pp. 97-8
K&R2 Sec. 5.4 p. 102
ANSI Sec. 4.1.5, Sec. 3.2.2.3
ISO Sec. 7.1.6, Sec. 6.2.2.3
Rationale Sec. 4.1.5
H&S Sec. 5.3.2 p. 122, Sec. 11.1 p. 292

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback