What does a type followed by
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
int_t anInt;
I see it a lot in C code meant to deal closely with hardware—I can't help but think that they're related.
As Douglas Mayle noted, it basically denotes a type name. Consequently, you would be ill-advised to end variable or function names with ' _t
' since it could cause some confusion. As well as size_t
, the C89 standard defines wchar_t
, off_t
, ptrdiff_t
, and probably some others I've forgotten. The C99 standard defines a lot of extra types, such as uintptr_t
, intmax_t
, int8_t
, uint_least16_t
, uint_fast32_t
, and so on. These new types are formally defined in <stdint.h>
but most often you will use <inttypes.h>
which (unusually for standard C headers) includes <stdint.h>
. It ( <inttypes.h>
) also defines macros for use with the printf()
and scanf()
.
As Matt Curtis noted, there is no significance to the compiler in the suffix; it is a human-oriented convention.
However, you should also note that POSIX defines a lot of extra type names ending in ' _t
', and reserves the suffix for the implementation. That means that if you are working on POSIX-related systems, defining your own type names with the convention is ill-advised. The system I work on has done it (for more than 20 years); we regularly get tripped up by systems defining types with the same name as we define.
这是一个用于命名数据类型的约定,例如typedef
:
typedef struct {
char* model;
int year;
...
} car_t;
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 matchs POSIX 1003.1 Issues 6 and you defined a type foo_t
. POSIX 1003.1 Issues 7 is eventually released with a new 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 for why not using typedef. Hope this help!
链接地址: http://www.djcxy.com/p/14018.html下一篇: 什么是类型跟着