int8_t,uint8_t等等数据类型中的_t是什么意思

_t 代表type,是被typedef定义的。

一般不推荐使用,和什么POSIX的约定有关。用也可以,加前缀比较安全,如ngx_str_t.

参考文献:

[1] http://www.cnblogs.com/swnuwangyun/archive/2011/06/10/2077389.html

[2] http://stackoverflow.com/questions/3225386/if-i-do-a-typedef-in-c-or-c-when-should-i-add-t-at-the-end-of-typedefed

[3] http://stackoverflow.com/questions/231760/what-does-a-type-followed-by-t-underscore-t-represent

 

———————-部分回答————–

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.

shareimprove this answer
edited Sep 8 ’09 at 1:18

Orion Edwards
59.6k32155227
answered Oct 23 ’08 at 22:29

Jonathan Leffler
343k45357637

it seems reasonable that OS and common runtime libraries define types with genericish names; but shouldn’t your company’s types also be prepended with a prefix or something? – Toybuilder Oct 23 ’08 at 22:38

Yes, they should. Unfortunately, I wasn’t in charge of the naming convention at the time when it was used. And global search and replace isn’t something that’s sanctioned – though it could be used to fix a lot of problems, even in a monstrous code base. – Jonathan Leffler Oct 23 ’08 at 23:29
3
I use _type instead of _t on my typedefs precisely to avoid that. – CesarB Oct 23 ’08 at 23:51

@Jonathan Leffler — What naming convention would you use for user-defined types? – J. Andrew Laughlin Apr 18 ’11 at 21:43
5
@Andrew: if you have a convenient abbreviation to use as a prefix, then you may be safe to use abbr_xxxxx_t type names. Without such a prefix, you may get caught 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. The system I mainly work on tends to live dangerously and use _t anyway, but it has bitten us on occasions. I tend to use CamelCase without a suffix for my own work; my functions are usually all lower-case. – Jonathan Leffler Apr 18 ’11 at 22:45

@JonathanLeffler, I’ve started using that convention, CamelCase for types, lower_case for functions. I searched this question hoping I wasn’t the only one. Thanks for the validation! – Austin Mullins Jun 13 ’14 at 22:26

 

1 评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注