Preferred types

Randall Maas 1/16/2011 5:04:21 PM

Continuing on with some thoughts in the coding style, I ramble a bit about the kinds of types to be preferred.

Integer types

signedunsigned
specific width (_XX_ bits)intXX_tuintXX_t
genericint, except when reasonable to assume overflow handling, then use long unsigned, except when reasonable to assume overflow handling, then use unsigned long.

Commentary

The names are the C99 standard. But I've seen projects consistently use intXX and T_INTXX, or event INTXXS and INTXXU (!). I'd suggest that new projects be consistent with the C99 Types

I've seen this table produced in many places, so I'm not being original. But I've seen many shops that have created their own types for different signed and unsigned integers. It seems silly when there are perfectly good types, and type names in the standard.

Floating point types

Floating point types should be double.

Characters and strings

char

should only be used to represent characters, and nothing should be assumed about it's sign.

Characters might use char type, or a "wider" type, as appropriate. Similarly, strings might use const char*, but strings might use a more specific type.

(Personally, I'm of the opinion that text strings should be zero-terminated UTF-8 strings without embedded nulls. But there is plenty of software out there that uses UNICODE, with its encumbent flaws.)

Pointers

Pointer should be to the specific type, if known. A generic type should be void*. const should be employed whereever possible, and dropped only when required.

Note: the * is part of the type.

Note on type qualifiers

Qualifiers like "const," "static," "extern," and so forth, should always go before the thing they modify, not after.

Update

Updated in the next entry