后跟_t(下划线-t)的类型代表什么? [英] What does a type followed by _t (underscore-t) represent?
问题描述
这似乎是一个简单的问题,但我无法通过 Stack Overflow 搜索或 Google 找到它.类型后跟 _t
是什么意思?比如
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;
我在 C 代码中经常看到它与硬件密切相关——我不禁认为它们是相关的.
I see it a lot in C code meant to deal closely with hardware—I can't help but think that they're related.
推荐答案
正如 Douglas Mayle 所指出的,它基本上表示一个类型名称.因此,不建议您将变量或函数名称以 '_t
' 结尾,因为这可能会引起一些混淆.除了 size_t
,C89 标准还定义了 wchar_t
、off_t
、ptrdiff_t
,可能还有一些我已经定义过的忘记了.C99标准定义了很多额外的类型,例如uintptr_t
、intmax_t
、int8_t
、uint_least16_t
、uint_fast32_t
等等.这些新类型在
中正式定义,但大多数情况下,您将使用
,其中(通常用于标准 C 头文件)包括
.它 (
) 还定义了用于 printf()
和 scanf()
的宏.
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.
但是,您还应该注意 POSIX 定义了很多以 ' 结尾的额外类型名称_t
',并为实现保留后缀.这意味着如果您正在处理与 POSIX 相关的系统,那么使用约定定义您自己的类型名称是不明智的.我工作的系统已经做到了(20 多年);我们经常被定义与我们定义的名称相同的类型的系统绊倒.
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.
这篇关于后跟_t(下划线-t)的类型代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!