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

5. Types

Boolean types
Don't use an enumeration type or #defines for Boolean variables or values. Declare them as an appropriate built-in type such as int.

intervals
To define a range or an interval, use exclusive upper bounds, not inclusive, so that upper - lower is the number of values inside the interval. This practice also simplifies iteration over the interval with for by reducing the amount of necessary thought: `<' can be used instead of the less-common `<='.

bit masks
Use only unsigned types for bit manipulation. This includes constants: write x & 7u, not x & 7. Bitwise operations are less well-defined on signed types.

typedef
In general, avoid using typedef. Code is clearer if the actual type is visible at the point of declaration.

Do not declare a typedef for a struct, union, or enum. Do not declare a typedef for pointer types, because they can be very confusing.

A function type is a good use for a typedef because it can clarify code. Give a function typedef a lowercase name with the ending _func. The type should be a function type, not a pointer-to-function type. That way, the typedef name can be used to declare function prototypes. (It cannot be used for function definitions, because that is explicitly prohibited by the ANSI standard.)


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

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