Numerical constants should not be coded directly.
The
#define
feature of the C preprocessor should be used to
give constants meaningful names.
Symbolic constants make the code easier to read.
Defining the value in one place
also makes it easier to administer large programs since the
constant value can be changed uniformly by changing only the
define.
The enumeration data type is a better way to declare variables
that take on only a discrete set of values, since
additional type checking is often available.
At the very least, any directly-coded numerical constant must have a
comment explaining the derivation of the value.
Constants should be defined consistently with their use;
e.g. use
540.0
for a float instead of
540
with an implicit float cast.
There are some cases where the constants 0 and 1 may appear as
themselves instead of as defines.
For example if a
for
loop indexes through an array, then
for (i = 0; i < ARYBOUND; i++)
door_t *front_door = opens(door[i], 7);
if (front_door == 0)
error("can't open %s\n", door[i]);
front_door
is a pointer.
When a value is a pointer it should be compared to
NULL
instead of 0.
NULL
is available
as part of the standard I/O library's header file stdio.h
and stdlib.h.
Even simple values like 1 or 0 are often better expressed using
defines like
TRUE
and
FALSE
(sometimes
YES
and
NO
read better).
Simple character constants should be defined as character literals
rather than numbers.
Non-text characters are discouraged as non-portable.
If non-text characters are necessary,
particularly if they are used in strings,
they should be written using a escape character of three octal digits
rather than one
(e.g.,
`\007
').
Even so, such usage should be considered machine-dependent and treated
as such.