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

Am I confused when should add final _t to typedef 'ed types?

For example, should I do this:

 typedef struct image image_t; 

or that:

 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 that:

 typdef enum { ARRAY_CLOSED, ARRAY_OPEN, ARRAY_HALFOPEN } array_type; 

Please enlighten me.

Thanks, Boda Sido.

+44
c ++ c typedef
Jul 12 2018-10-12T00:
source share
6 answers

On POSIX, names ending in _t are reserved, so if you are targeting a POSIX system (like Linux), you should not end your types with _t .

+52
Jul 12 '10 at 1:45
source share

I personally despise the _t agreement. As long as you are consistent, it really doesn't matter.

Please note that (as the other answers indicate here), if you are coding any other standard, for example POSIX, you need to check if this standard is good before using such names.

+17
Jul 12 2018-10-12T00:
source share

When should _t be used? Never? It is reserved by the main standard (POSIX), and even if it is not, your code may someday be used in a POSIX environment, so using _t is a bad idea.

I would like to further say that overuse of typedef bad overall. If your type is struct , union or enum , use these keywords when you declare variables, and this makes your code more understandable. The use of typedef best reserved to make the underlying type invisible for abstraction / encapsulation purposes. Some great examples from the C standard: size_t , int32_t , mbstate_t and stdio FILE .

Some of the worst abuses of typedef relate to the Windows API ( WORD , DWORD , INT , LPSTR , etc.) and glib ( gint , gchar , etc.). Creating duplicates of standard C types with the same intended use is just confusing and serves to block developers in your library / platform, polluting all the code with these non-standard type names.

+7
Jul 12 '10 at 8:12
source share

I use suffixes to improve readability: _t for typedef and _e for enumerations from 25/30 ... sometimes I use _st when typedef defines the structure.

I think it’s good practice to make the code readable and standardized, then I find the right to use suffixes! Also, to date, I have not found an official POSIX document stating that the suffix _t is reserved.

Old stdio.h contains _t ... See grep -i "_t;" stdio.h :) I think the POSIX standard is β€œa bit” younger than C!

+3
Apr 12 '15 at 13:19
source share

I use the suffix _t for enumerations and primitive types to distinguish them from variables. I put them in namespaces, so I don't need to reserve _t .

To justify this. Very often, a variable name is a hint of a typed type. Like std::size_t size; , array_t array , etc. I found it easier to find a decent name for the variable when the type contains the suffix _t . It also reminds me that this is a typed primitive, and not some other beast, such as a class.

0
Jul 12 2018-10-12T00:
source share

Choose good names for your types, as well as your variables, functions, and everything else. A good name does not contain redundant information that complicates the reading of code. _t never helps you if you have a good name to start with.

By the way: typedef image image; makes no sense, as it just makes a typedef image for itself.

0
Jul 12 '10 at 2:17
source share



All Articles