USDA ARS

Types

"typedef"s in IPW are used mainly as a portability tool. Where portable uses exist for the standard C data types, those types are used directly: loop counters, file descriptors, and function return codes are all "int"s; character strings are stored in arrays of, or manipulated by pointers to, "char"s; etc. This is intended to help make IPW code more readable to programmers with previous C experience. However, there are some circumstances where "typedef"s contribute significantly to readability.

Boolean variables should always have the type "bool_t", even though C by definition treats boolean expressions as having type "int".

If standard C types are being used in an especially restricted context, then the comment at the end of the corresponding declaration should mention the restriction. One standard way of doing this is to indicate the subrange of permissible values [Plum 1984].

Example:

float   azimuth;        /* radians: -pi .. pi */

Default typing (allowing function values and formal arguments to default to "int") should NEVER be used; this is one of the most common sources of bugs in C code.

The "void" type should always be used to cast the result of function whose value is ignored; this forces the programmer to acknowledge that a return value is being deliberately discarded.

All structures should be "typedef"d, and thereafter only referred to using the "typedef" type. IPW image headers are typical examples of this; see any of the "$IPW/h/xxxh.h" header files. Note that in a "typedef"d structure the tag is superfluous and should be omitted, unless the structure is self-referential, in which case the tag will be needed in one or more member declarations.

IPW programmers are encouraged to create their own types if doing so contributes to the readability of the program. They should first familiarize themselves with the default types provided in the header file "$IPW/h/_types.h", and in any other specialized header files they are using.


IPW documentation / Last revised 20 May 2009 / IPW web site