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.
The _t usually wraps an opaque type definition. The requirement that additional types defined in this section end in "_t" was prompted by the problem of name space pollution. It is difficult to define a type (where that type is not one defined by POSIX.
The typedef is a keyword used in C programming to provide some meaningful names to the already existing variable in the C program. It behaves similarly as we define the alias for the commands. In short, we can say that this keyword is used to redefine the name of an already existing variable.
Explanation: The keyword typedef is used to give an alternate name to an existing data type. Hence hello is the new name for enum good.
PLEASE don't typedef structs in C, it needlessly pollutes the global namespace which is typically very polluted already in large C programs. Also, typedef'd structs without a tag name are a major cause of needless imposition of ordering relationships among header files.
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
.
I personally despise the _t
convention. So long as you are consistent, it really does not matter, however.
Note that (as other answers here indicate) if you are coding to some other standard, such as POSIX, you need to check if it's okay in that standard before using such names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With