c/c++: uint8_t uint16_t uint32_t uint64_t size_t ssize_t数据类型

川长思鸟来 2022-08-10 12:55 327阅读 0赞

原文写的不错,转来收藏,转自: http://wangyisouhuxin.blog.163.com/blog/static/761966592011072348700/?fromdm&fromSearch&isFromSearchEngine=yes

在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等。咋一看,好像是个新的数据类型,不过C语言(nesc是C的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样的疑问。论坛上就有人问:以*_t结尾的类型是不是都是long型的?在baidu上查一下,才找到答案,这时才发觉原来自己对C掌握的太少。

那么_t的意思到底表示什么?具体的官方答案没有找到,不过我觉得有个答案比较接近。它就是一个结构的标注,可以理解为type/typedef的缩写,表示它是通过typedef定义的,而不是其它数据类型。

uint8_t,uint16_t,uint32_t等都不是什么新的数据类型,它们只是使用typedef给类型起的别名,新瓶装老酒的把戏。不过,不要小看了typedef,它对于你代码的维护会有很好的作用。比如C中没有bool,于是在一个软件中,一些程序员使用int,一些程序员使用short,会比较混乱,最好就是用一个typedef来定义,如:
typedef char bool;

一般来说,一个C的工程中一定要做一些这方面的工作,因为你会涉及到跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以让你最有效的维护你的代码。为了用户的方便,C99标准的C语言硬件为我们定义了这些类型,我们放心使用就可以了。

按照posix标准,一般整形对应的*_t类型为:
1字节 uint8_t
2字节 uint16_t
4字节 uint32_t
8字节 uint64_t

附:inttypes.h的内容(不同的服务器会有不同的源文件结构,但原理是一样的,我这里sun服务器inttypes.h引用了int_type.h)

[cpp] view plain copy

  1. bash-3.00$ vi int_types.h
  2. “int_types.h” [Read only] 176 lines, 4367 characters
  3. /*
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * Use is subject to license terms.
  6. */
  7. #ifndef _SYS_INT_TYPES_H
  8. #define _SYS_INT_TYPES_H
  9. #pragma ident “@(#)int_types.h 1.10 04/09/28 SMI”
  10. /*
  11. * This file, , is part of the Sun Microsystems implementation
  12. * of defined in the ISO C standard, ISO/IEC 9899:1999
  13. * Programming language - C.
  14. *
  15. * Programs/Modules should not directly include this file. Access to the
  16. * types defined in this file should be through the inclusion of one of the
  17. * following files:
  18. *
  19. * Provides only the “_t” types defined in this
  20. * file which is a subset of the contents of
  21. * . (This can be appropriate for
  22. * all programs/modules except those claiming
  23. * ANSI-C conformance.)
  24. *
  25. * Provides the Kernel and Driver appropriate
  26. * components of .
  27. *
  28. * For use by applications.
  29. *
  30. * See these files for more details.
  31. */
  32. #include
  33. #ifdef __cplusplus
  34. extern “C” {
  35. #endif
  36. /*
  37. * Basic / Extended integer types
  38. *
  39. * The following defines the basic fixed-size integer types.
  40. *
  41. * Implementations are free to typedef them to Standard C integer types or
  42. * extensions that they support. If an implementation does not support one
  43. * of the particular integer data types below, then it should not define the
  44. * typedefs and macros corresponding to that data type. Note that int8_t
  45. * is not defined in -Xs mode on ISAs for which the ABI specifies “char”
  46. * as an unsigned entity because there is no way to define an eight bit
  47. * signed integral.
  48. */
  49. #if defined(_CHAR_IS_SIGNED)
  50. typedef char int8_t;
  51. #else
  52. #if defined(__STDC__)
  53. typedef signed char int8_t;
  54. #endif
  55. #endif
  56. typedef short int16_t;
  57. typedef int int32_t;
  58. #ifdef _LP64
  59. #define _INT64_TYPE
  60. typedef long int64_t;
  61. #else /* _ILP32 */
  62. #if defined(_LONGLONG_TYPE)
  63. #define _INT64_TYPE
  64. typedef long long int64_t;
  65. #endif
  66. #endif
  67. typedef unsigned char uint8_t;
  68. typedef unsigned short uint16_t;
  69. typedef unsigned int uint32_t;
  70. #ifdef _LP64
  71. typedef unsigned long uint64_t;
  72. #else /* _ILP32 */
  73. #if defined(_LONGLONG_TYPE)
  74. typedef unsigned long long uint64_t;
  75. #endif
  76. #endif
  77. /*
  78. * intmax_t and uintmax_t are to be the longest (in number of bits) signed
  79. * and unsigned integer types supported by the implementation.
  80. */
  81. #if defined(_INT64_TYPE)
  82. typedef int64_t intmax_t;
  83. typedef uint64_t uintmax_t;
  84. #else
  85. typedef int32_t intmax_t;
  86. typedef uint32_t uintmax_t;
  87. #endif
  88. /*
  89. * intptr_t and uintptr_t are signed and unsigned integer types large enough
  90. * to hold any data pointer; that is, data pointers can be assigned into or
  91. * from these integer types without losing precision.
  92. */
  93. #if defined(_LP64) || defined(_I32LPx)
  94. typedef long intptr_t;
  95. typedef unsigned long uintptr_t;
  96. #else
  97. typedef int intptr_t;
  98. typedef unsigned int uintptr_t;
  99. #endif
  100. /*
  101. * The following define the fastest integer types that can hold the
  102. * specified number of bits.
  103. */
  104. #if defined(_CHAR_IS_SIGNED)
  105. typedef char int_fast8_t;
  106. #else
  107. #if defined(__STDC__)
  108. typedef signed char int_fast8_t;
  109. #endif
  110. #endif
  111. typedef int int_fast16_t;
  112. typedef int int_fast32_t;
  113. #ifdef _LP64
  114. typedef long int_fast64_t;
  115. #else /* _ILP32 */
  116. #if defined(_LONGLONG_TYPE)
  117. typedef long long int_fast64_t;
  118. #endif
  119. #endif
  120. typedef unsigned char uint_fast8_t;
  121. typedef unsigned int uint_fast16_t;
  122. typedef unsigned int uint_fast32_t;
  123. #ifdef _LP64
  124. typedef unsigned long uint_fast64_t;
  125. #else /* _ILP32 */
  126. #if defined(_LONGLONG_TYPE)
  127. typedef unsigned long long uint_fast64_t;
  128. #endif
  129. #endif
  130. /*
  131. * The following define the smallest integer types that can hold the
  132. * specified number of bits.
  133. */
  134. #if defined(_CHAR_IS_SIGNED)
  135. typedef char int_least8_t;
  136. #else
  137. #if defined(__STDC__)
  138. typedef signed char int_least8_t;
  139. #endif
  140. #endif
  141. typedef short int_least16_t;
  142. typedef int int_least32_t;
  143. #ifdef _LP64
  144. typedef long int_least64_t;
  145. #else /* _ILP32 */
  146. #if defined(_LONGLONG_TYPE)
  147. typedef long long int_least64_t;
  148. #endif
  149. #endif
  150. typedef unsigned char uint_least8_t;
  151. typedef unsigned short uint_least16_t;
  152. typedef unsigned int uint_least32_t;
  153. #ifdef _LP64
  154. typedef unsigned long uint_least64_t;
  155. #else /* _ILP32 */
  156. #if defined(_LONGLONG_TYPE)
  157. typedef unsigned long long uint_least64_t;
  158. #endif
  159. #endif
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /* _SYS_INT_TYPES_H */

同理在types.h文件中会有size_t ssize_t的定义,篇幅较长不贴出了。

发表评论

表情:
评论列表 (有 0 条评论,327人围观)

还没有评论,来说两句吧...

相关阅读

    相关 uint16_t

    uint16\_t 是一种数据类型,它表示无符号 16 位整数。这种类型的整数值的范围是 0 到 65535,即最多可以表示 2^16 个不同的数字。uint16\_t 类型的