What's the recommended approach to typedefs for standard types in C?
For example at the start of my project I created typedefs to use the smallest types possible for their purpose. The main intention was to allow for easy modification of the types in the case where the numerical ranges used became too large for the type without having to do a global search+replace on the entire project. But after six months I'm not satisfied and think there must be a better way.
These are the types I have defined:
/* type to be used for storing map data within
n*n array */
typedef unsigned char map_t;
/* type to be used for storing coordinates in map.
min 0,max 32 (MAP_W,MAP_H defaults). note: no
longer requires signed. */
typedef unsigned char xy_t;
/* type to be used for counters, ie moves_remaining
has max 2000 */
typedef signed int ctr_t;
/* type to be used for storing the level number,
requires signed. */
typedef signed short lvl_t;
/* type to be used for storing contact types,
see actions.h CONTACT */
typedef signed short ct_t;
/* small unsigned type, miscellaneous */
typedef unsigned char su_t;
/* small signed type, miscellaneous */
typedef signed char ss_t;
struct xy
{
xy_t x;
xy_t y;
};
struct scrxy
{
int x;
int y;
};
struct xy_offset
{
ss_t x;
ss_t y;
};
Some code, instead of using a signed type and checking for n < 0 && n > max_val
uses an unsigned type and only checks for n > max_val
where max_val
is less the T_MAX
although I've since discovered this behaviour is actually undefined.
Should I remove all these typedef
s and use the standard types define in stdint.h
instead throughout my code (the sizes have stabilized fairly well now)?