If I do a `typedef` in C or C++, when should I add `_t` at the end of typedef'ed type?

bodacydo picture bodacydo · Jul 12, 2010 · Viewed 14.4k times · Source

I am confused when should I add the trailing _t to typedef'ed types?

For example, should I do this:

typedef struct image image_t;

or this:

typedef struct image image;

What are the general rules?

Another example, should I do this:

typdef enum { ARRAY_CLOSED, ARRAY_OPEN, ARRAY_HALFOPEN } array_type_t;

or this:

typdef enum { ARRAY_CLOSED, ARRAY_OPEN, ARRAY_HALFOPEN } array_type;

Please enlighten me.

Thanks, Boda Cydo.

Answer

James McNellis picture James McNellis · Jul 12, 2010

In POSIX, names ending with _t are reserved, so if you are targeting a POSIX system (e.g., Linux), you should not end your types with _t.