What does a type followed by _t (underscore-t) represent?

后端 未结 10 577
萌比男神i
萌比男神i 2020-11-22 07:15

This seems like a simple question, but I can\'t find it with the Stack Overflow search or Google. What does a type followed by a _t mean? Such as



        
相关标签:
10条回答
  • 2020-11-22 07:26

    The _t usually wraps an opaque type definition.

    GCC merely add names that end with _t to the reserved namespace you may not use, to avoid conflicts with future versions of Standard C and POSIX (GNU C library manual). After some research, I finally found the correct reference inside the POSIX Standard (1003.1, Rationale (Informative)):

    B.2.12 Data Types

    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 IEEE Std 1003.1-2001) in one header file and use it in another without adding symbols to the name space of the program. To allow implementors to provide their own types, all conforming applications are required to avoid symbols ending in ‘‘_t’’, which permits the implementor to provide additional types. Because a major use of types is in the definition of structure members, which can (and in many cases must) be added to the structures defined in IEEE Std 1003.1-2001, the need for additional types is compelling.

    In a nutshell, the Standard says that there are good chances of extending the Standard types' list, therefore the Standard restricts the _t namespace for its own use.

    For instance, your program matches POSIX 1003.1 Issues 6 and you defined a type foo_t. POSIX 1003.1 Issues 7 is eventually released with a newly defined type foo_t. Your program does not match the new version, which might be a problem. Restricting the _t usage prevents from refactoring the code. Thus, if you aim to a POSIX compliancy, you should definitely avoid the _t as the Standard states it.

    Side note: personally, I try to stick to POSIX because I think it gives good basics for clean programming. Moreover, I am pretty fond of Linux Coding Style (chapter 5) guidelines. There are some good reasons why not using typedef. Hope this help!

    0 讨论(0)
  • 2020-11-22 07:30

    It means type. size_t is the size type.

    0 讨论(0)
  • 2020-11-22 07:31

    The _t does not inherently have any special meaning. But it has fallen into common use to add the _t suffix to typedef's.

    You may be more familiar with common C practices for variable naming... This is similar to how it's common to stick a p at the front for a pointer, and to use an underscore in front of global variables (this is a bit less common), and to use the variable names i, j, and k for temporary loop variables.

    In code where word-size and ordering is important, it's very common to use custom defined types that are explicit, such as BYTE WORD (normally 16-bit) DWORD (32-bits).

    int_t is not so good, because the definition of int varies between platforms -- so whose int are you conforming to? (Although, these days, most PC-centric development treats it as 32 bits, much stuff for non-PC development still treat int's as 16 bits).

    0 讨论(0)
  • 2020-11-22 07:33

    If you're dealing with hardware interface code, the author of the code you're looking at might have defined int_t to be a specific size integer. The C standard doesn't assign a specific size to the int type (it depends on your compiler and target platform, potentially), and using a specific int_t type would avoid that portability problem.

    This is a particularly important consideration for hardware interface code, which may be why you've first noticed the convention there.

    0 讨论(0)
  • 2020-11-22 07:37

    It is a standard naming convention for data types, usually defined by typedefs. A lot of C code that deals with hardware registers uses C99-defined standard names for signed and unsigned fixed-size data types. As a convention, these names are in a standard header file (stdint.h), and end with _t.

    0 讨论(0)
  • 2020-11-22 07:42

    It's a convention used for naming data types, e.g with typedef:

    
    typedef struct {
      char* model;
      int year;
    ...
    } car_t;
    
    
    0 讨论(0)
提交回复
热议问题