[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

11. Expressions

white space
Put a space on both sides of all binary operators and the ternary operator.

Exceptions: `()', `[]', `->', `.', and `,'. The `()' and `,' operators have their own rules explained elsewhere. The others should not have white space on either side.

casts
Avoid casts. In particular, never cast the return value of malloc().

sizeof
Where practical, measure the size of objects, not of types: int *x = malloc (sizeof *x);.

The value of sizeof (char) is guaranteed to be 1, so don't ever write that expression.

tests for zero
Write tests for numeric zero as number == 0, for the string null terminator as character == '\0', and for the null pointer as pointer == NULL, and similarly for nonzero. Don't write tests as plain if (variable).

floating-point comparisons
Avoid direct floating-point comparisons, especially tests for equality and inequality. In fact, avoid floating point as much as practical.

array size
Use the syntax sizeof array / sizeof *array to calculate the number of elements in an array.

relational operands
When using a relational operator like `<' or `==', put an expression or variable argument on the left and a constant argument on the right. Exception: tests for mathematical a < x < b, and similar, should be written as a < x && x < b.

assignment
Use assignments with `=' and related operators only as statements by themselves. Rare exceptions occur when this forces code to become excessively awkward.

long strings
Use the ANSI C method to split long strings between lines: write a separate string on each line and let the C compiler concatenate juxtaposed strings. Split strings after, not before, white space.

i18n
Write code looking forward to future internationalization with GNU gettext. Most importantly, surround English text strings with _(...) in order to avoid the need to go through all the code again later. It is acceptable to write #define _(TEXT) (TEXT) at the top of the source file if full i18n is not yet desired. See section `Top' in GNU gettext Manual, for more details.

bit masks
Generally prefer octal to hexadecimal for bit masks, because it is usually easier to see bit patterns in octal. Use hexadecimal if it is clearly superior.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Ben Pfaff on September, 11 2001 using texi2html