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.
The C gurus have a method to their madness when it comes to naming these variables. The _t stands for something. It's very consistent on purpose. The _t implies a typedef, a defined data type. The typedef is based on an existing type.
The _t data types are typedef types in the stdint. h header, while int is a built-in fundamental data type to C. The _t datatypes are only available if stdint. h exists. The fundamental data types like int , however, are guaranteed to exist.
typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.
It means type. size_t is the size type. 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...
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.
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>.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With