What does the _t suffix mean and when to use it?

Asked

Viewed 235 times

9

I see in many codes some variables with the suffix _t. There are a lot of examples in the standard C library like size_t, int32_t, mbstate_t.

What is the use, and when to use this suffix?

2 answers

6


It is a convention indicating that that name represents a type of data (t of type). It is used not to be confused with other code identifiers.

As far as I know it was created to avoid conflicts with existing code since originally these types did not exist and someone could have used these names on something in the code, which would have been a problem in the compilation. Some consider that it is only to make it clearer that it is a type and not a variable.

In your codes it is not usually so useful to use and it is rare to see people using in their types. The same goes for libraries. Personally I find almost all C conventions bad and poorly thought out, some understand why, but I don’t agree.

I prefer to use uppercase types.

4

To complement the @Maniero response:

It’s a (human) convention to say it’s like.

The C89 standard defines size_t, wchar_t, off_t, ptrdiff_t, among others. C99 defines things as uintptr_t, intmax_t, int8_t, uint_least16_t, uint_fast32_t, etc..

Files of interest, to take a peek at the settings: stdint.h, inttypes.h, stdint.h

Example:

typedef struct {
  char* model;
  int year;
...
} car_t;

See a mention in this reference:

https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html

And similar question in Sozão:

https://stackoverflow.com/q/231760/916193

Browser other questions tagged

You are not signed in. Login or sign up in order to post.