What is the suffix _t and when to use it?

5

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

How useful, and when to use this suffix?

    
asked by anonymous 29.09.2016 / 15:48

2 answers

4

This is a convention that indicates that the name represents a data type (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 these types originally did not exist and someone could have used those names in something in the code, which would give compile trouble. Some consider it just to make it clearer that it is a type rather than a variable.

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

    
29.09.2016 / 15:56
2

To complement @bigown's response:

It's a (human) convention to say it's kind.

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

Files of interest, for a peek at the definitions: stdint.h , inttypes.h , stdint.h

Example:

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

See a reference to this reference:

link

And similar question in the SOzão:

link

    
29.09.2016 / 15:58