zoukankan      html  css  js  c++  java
  • c/c++: uint8_t uint16_t uint32_t uint64_t size_t ssize_t数据类型

    在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)

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

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

  • 相关阅读:
    vue-router路由知识补充
    vue-router路由模式详解
    Linq To Sql的各种查询
    消息队列系列(四):Rabbitmq常用命令行
    产品发布之我见
    利用LogParser分析IIS日志
    SqlServer_删除重复行只保留一条记录
    SqlServer_合并多个递归查询数据(CTE)
    rabbitmq_坑
    mongodb_性能监控
  • 原文地址:https://www.cnblogs.com/ainima/p/6331157.html
Copyright © 2011-2022 走看看