cnaming-conventionstypes

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


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.


Solution

  • 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, time_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 _t suffix for the implementation. That means that if you are working on POSIX-related systems, defining your own type names with the _t suffix 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 have a convenient abbreviation to use as a prefix, then you may be safe to use abbr_xxxxx_t type names (though POSIX could decide to use your prefix). Without such a prefix, you may get caught at any time. Generally, the standardized _t types use all lower-case (FILE and DIR are two exceptions, twice - all caps, and no _t), so you could use CamelCase_t with moderate safety, with or without the leading caps. I tend to use CamelCase (or is that PascalCase with camelCase not having the leading capital letter?) without a suffix for my own work; my functions are usually all lower-case.