Continuing on with some thoughts in the coding style, I ramble a bit about the kinds of types to be preferred.
signed | unsigned | |
---|---|---|
specific width (_XX_ bits) | intXX_t | uintXX_t |
generic | int , except when reasonable to assume overflow handling, then use long | unsigned , except when reasonable to assume overflow handling, then use unsigned long . |
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 should be double
.
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.)
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.
Qualifiers like "const," "static," "extern," and so forth, should always go before the thing they modify, not after.
Updated in the next entry