C标准库源码解剖(11):扩展整数类型stdint.h和inttypes.h

C语言的基本精神是让实现选择标准类型的长度,但是这种指导思想使可移植代码难以编写。C99中引入了stdint.h和inttypes.h,对整数类型的定义和格式转换进行了规范。这种扩展整数类型的定义非常清晰,从类型名字上就可以看出它的长度,这有利于编写可移植的代码。stint.h对整数类型进行定义,inttypes.h包含了stdint.h并增加了可移植的格式控制串和转换函数。
    1、stdint.h: 定义标准的扩展整数类型。包括准确长度类型intN_t、最小长度类型int_leastN_t、快速长度类型int_fastN_t、指针长度类型intptr_t、最大长度类型intmax_t(N为类型宽度)。
    定义规则:
    1)类型的长度用宽度N参数化,如intN_t,N常常有8,16,32,64。
    2)若要定义某种类型,则该类型的带符号和无符号类型、类型长度都要定义。如定义int32_t,则有uint32_t类型。
    3)要用...MIN和...MAX宏定义类型的范围。如INT32_MIN,INT32_MAX,UINT32_MAX。
    4)在inttypes.h要用PRIcKN和SCNcKN定义打印该类型的printf和scanf格式控制字符串。c表示转换操作符,有d,i,o,u,x或X;K表示种类,为空或者LEAST,FAST,PTR,MAX;N是该类型的宽度。

  1. /* ISO C99: 7.18 整数类型 <stdint.h>  */  
  2. #ifndef _STDINT_H  
  3. #define _STDINT_H   1  
  4. #include <features.h>  
  5. #include <bits/wchar.h>  
  6. #include <bits/wordsize.h>  
  7. /* 准确类型:指定长度的准确类型  */  
  8. /* 有符号  */  
  9. /* 有一些与<sys/types.h>中众所周知的inet代码重叠 */  
  10. #ifndef __int8_t_defined  
  11. # define __int8_t_defined  
  12. typedef signed char     int8_t; /* 8位的准确长度类型int8_t=signed char */  
  13. typedef short int       int16_t; /* 16位的准确长度类型 */  
  14. typedef int         int32_t;  
  15. # if __WORDSIZE == 64   
  16. typedef long int        int64_t;  /*  64位平台上64位的准确类型为long */  
  17. # else  
  18. __extension__  
  19. typedef long long int       int64_t; /*  32位平台上64位的准确类型为long long */  
  20. # endif  
  21. #endif  
  22. /* 无符号  */  
  23. typedef unsigned char       uint8_t;  
  24. typedef unsigned short int  uint16_t;  
  25. #ifndef __uint32_t_defined  
  26. typedef unsigned int        uint32_t;  
  27. # define __uint32_t_defined  
  28. #endif  
  29. #if __WORDSIZE == 64  
  30. typedef unsigned long int   uint64_t;  
  31. #else  
  32. __extension__  
  33. typedef unsigned long long int  uint64_t;  
  34. #endif  
  35.   
  36. /* 最小类型:指定长度的最小类型  */  
  37. /* 有符号  */  
  38. typedef signed char     int_least8_t;  
  39. typedef short int       int_least16_t;  
  40. typedef int         int_least32_t;  
  41. #if __WORDSIZE == 64  
  42. typedef long int        int_least64_t;  
  43. #else  
  44. __extension__  
  45. typedef long long int       int_least64_t;  
  46. #endif  
  47. /* 无符号  */  
  48. typedef unsigned char       uint_least8_t;  
  49. typedef unsigned short int  uint_least16_t;  
  50. typedef unsigned int        uint_least32_t;  
  51. #if __WORDSIZE == 64  
  52. typedef unsigned long int   uint_least64_t;  
  53. #else  
  54. __extension__  
  55. typedef unsigned long long int  uint_least64_t;  
  56. #endif  
  57.   
  58. /* 快速类型:指定长度的最快类型  */  
  59. /* 有符号  */  
  60. typedef signed char     int_fast8_t;  
  61. #if __WORDSIZE == 64       /* 64位平台 */  
  62. typedef long int        int_fast16_t;  
  63. typedef long int        int_fast32_t;  
  64. typedef long int        int_fast64_t;  
  65. #else     /* 32位平台 */  
  66. typedef int         int_fast16_t; /* 16位和32位的最快类型均为int */  
  67. typedef int         int_fast32_t;  
  68. __extension__  
  69. typedef long long int       int_fast64_t;  
  70. #endif  
  71. /* 无符号  */  
  72. typedef unsigned char       uint_fast8_t;  
  73. #if __WORDSIZE == 64  
  74. typedef unsigned long int   uint_fast16_t;  
  75. typedef unsigned long int   uint_fast32_t;  
  76. typedef unsigned long int   uint_fast64_t;  
  77. #else  
  78. typedef unsigned int        uint_fast16_t;  
  79. typedef unsigned int        uint_fast32_t;  
  80. __extension__  
  81. typedef unsigned long long int  uint_fast64_t;  
  82. #endif  
  83.   
  84. /* 通用指针类型:即void * 型指针的类型,64位平台上为long,32位平台上为int  */  
  85. #if __WORDSIZE == 64  
  86. # ifndef __intptr_t_defined  
  87. typedef long int        intptr_t;  
  88. #  define __intptr_t_defined  
  89. # endif  
  90. typedef unsigned long int   uintptr_t;  
  91. #else  
  92. # ifndef __intptr_t_defined  
  93. typedef int         intptr_t;  
  94. #  define __intptr_t_defined  
  95. # endif  
  96. typedef unsigned int        uintptr_t;  
  97. #endif  
  98.   
  99. /* 最大类型:指定长度的最大整数类型  */  
  100. #if __WORDSIZE == 64  
  101. typedef long int        intmax_t;  /* 64位平台上的最大整数类型为long */  
  102. typedef unsigned long int   uintmax_t;  
  103. #else  
  104. __extension__  
  105. typedef long long int       intmax_t;  /* 32位平台上的最大整数类型为long long */  
  106. __extension__  
  107. typedef unsigned long long int  uintmax_t;  
  108. #endif  
  109. /* ISO C99标准指出,在C++实现中这些宏应该只在被请求到的时候才定义 */  
  110. #if !defined __cplusplus || defined __STDC_LIMIT_MACROS  
  111. # if __WORDSIZE == 64  
  112. #  define __INT64_C(c)  c ## L  
  113. #  define __UINT64_C(c) c ## UL  
  114. # else  
  115. #  define __INT64_C(c)  c ## LL  
  116. #  define __UINT64_C(c) c ## ULL  
  117. # endif  
  118. /* 整数类型的范围  */  
  119. /* 有符号整数类型的最小值:-2**(N-1),其中最小负数-2**(N-1)=100...0没有对应正数,其反数还是自己  */  
  120. # define INT8_MIN       (-128)  
  121. # define INT16_MIN      (-32767-1)  
  122. # define INT32_MIN      (-2147483647-1)  
  123. # define INT64_MIN      (-__INT64_C(9223372036854775807)-1)  
  124. /* 有符号整数类型的最大值:2**(N-1)-1  */  
  125. # define INT8_MAX       (127)  
  126. # define INT16_MAX      (32767)  
  127. # define INT32_MAX      (2147483647)  
  128. # define INT64_MAX      (__INT64_C(9223372036854775807))  
  129. /* 无符号整数类型的最大值:2**(N-1)-1,注意有Min=-MAX-1  */  
  130. # define UINT8_MAX      (255)  
  131. # define UINT16_MAX     (65535)  
  132. # define UINT32_MAX     (4294967295U)  
  133. # define UINT64_MAX     (__UINT64_C(18446744073709551615))  
  134.   
  135. /* 有符号最小类型的最小值  */  
  136. # define INT_LEAST8_MIN     (-128)  
  137. # define INT_LEAST16_MIN    (-32767-1)  
  138. # define INT_LEAST32_MIN    (-2147483647-1)  
  139. # define INT_LEAST64_MIN    (-__INT64_C(9223372036854775807)-1)  
  140. /* 有符号最小类型的最大值  */  
  141. # define INT_LEAST8_MAX     (127)  
  142. # define INT_LEAST16_MAX    (32767)  
  143. # define INT_LEAST32_MAX    (2147483647)  
  144. # define INT_LEAST64_MAX    (__INT64_C(9223372036854775807))  
  145. /* 无符号最小类型的最大值  */  
  146. # define UINT_LEAST8_MAX    (255)  
  147. # define UINT_LEAST16_MAX   (65535)  
  148. # define UINT_LEAST32_MAX   (4294967295U)  
  149. # define UINT_LEAST64_MAX   (__UINT64_C(18446744073709551615))  
  150.   
  151. /* 有符号快速类型的最小值  */  
  152. # define INT_FAST8_MIN      (-128)  
  153. # if __WORDSIZE == 64  
  154. #  define INT_FAST16_MIN    (-9223372036854775807L-1)  
  155. #  define INT_FAST32_MIN    (-9223372036854775807L-1)  
  156. # else  
  157. #  define INT_FAST16_MIN    (-2147483647-1)  
  158. #  define INT_FAST32_MIN    (-2147483647-1)  
  159. # endif  
  160. # define INT_FAST64_MIN     (-__INT64_C(9223372036854775807)-1)  
  161. /* 有符号快速类型的最大值  */  
  162. # define INT_FAST8_MAX      (127)  
  163. # if __WORDSIZE == 64  
  164. #  define INT_FAST16_MAX    (9223372036854775807L)  
  165. #  define INT_FAST32_MAX    (9223372036854775807L)  
  166. # else  
  167. #  define INT_FAST16_MAX    (2147483647)  
  168. #  define INT_FAST32_MAX    (2147483647)  
  169. # endif  
  170. # define INT_FAST64_MAX     (__INT64_C(9223372036854775807))  
  171. /* 无符号快速类型的最大值  */  
  172. # define UINT_FAST8_MAX     (255)  
  173. # if __WORDSIZE == 64  
  174. #  define UINT_FAST16_MAX   (18446744073709551615UL)  
  175. #  define UINT_FAST32_MAX   (18446744073709551615UL)  
  176. # else  
  177. #  define UINT_FAST16_MAX   (4294967295U)  
  178. #  define UINT_FAST32_MAX   (4294967295U)  
  179. # endif  
  180. # define UINT_FAST64_MAX    (__UINT64_C(18446744073709551615))  
  181.   
  182. /* 指针类型(持有void*型指针)的范围  */  
  183. # if __WORDSIZE == 64  
  184. #  define INTPTR_MIN        (-9223372036854775807L-1)  
  185. #  define INTPTR_MAX        (9223372036854775807L)  
  186. #  define UINTPTR_MAX       (18446744073709551615UL)  
  187. # else  
  188. #  define INTPTR_MIN        (-2147483647-1)  
  189. #  define INTPTR_MAX        (2147483647)  
  190. #  define UINTPTR_MAX       (4294967295U)  
  191. # endif  
  192.   
  193. /* 有符号最大类型的最小值  */  
  194. # define INTMAX_MIN     (-__INT64_C(9223372036854775807)-1)  
  195. /* 符号最大类型的最大值  */  
  196. # define INTMAX_MAX     (__INT64_C(9223372036854775807))  
  197. /* 无符号最大类型的最大值 */  
  198. # define UINTMAX_MAX        (__UINT64_C(18446744073709551615))  
  199.   
  200. /* 其他整数类型的范围  */  
  201. /* ptrdiff_t类型的范围  */  
  202. # if __WORDSIZE == 64  
  203. #  define PTRDIFF_MIN       (-9223372036854775807L-1)  
  204. #  define PTRDIFF_MAX       (9223372036854775807L)  
  205. # else  
  206. #  define PTRDIFF_MIN       (-2147483647-1)  
  207. #  define PTRDIFF_MAX       (2147483647)  
  208. # endif  
  209. /* sig_atomic_t类型的范围  */  
  210. # define SIG_ATOMIC_MIN     (-2147483647-1)  
  211. # define SIG_ATOMIC_MAX     (2147483647)  
  212. /* size_t类型的范围  */  
  213. # if __WORDSIZE == 64  
  214. #  define SIZE_MAX      (18446744073709551615UL)  
  215. # else  
  216. #  define SIZE_MAX      (4294967295U)  
  217. # endif  
  218. /* wchar_t类型的范围  */  
  219. # ifndef WCHAR_MIN  
  220. /* 这些常量可能在<wchar.h>也定义了  */  
  221. #  define WCHAR_MIN     __WCHAR_MIN  
  222. #  define WCHAR_MAX     __WCHAR_MAX  
  223. # endif  
  224. /* wint_t类型的范围  */  
  225. # define WINT_MIN       (0u)  
  226. # define WINT_MAX       (4294967295u)  
  227. #endif  /* C++ && limit macros */  
  228. /* ISO C99标准指出,在C++实现中这些宏应该只在被请求到的时候才定义 */  
  229. #if !defined __cplusplus || defined __STDC_CONSTANT_MACROS  
  230. /* 有符号  */  
  231. # define INT8_C(c)  c  
  232. # define INT16_C(c) c  
  233. # define INT32_C(c) c  
  234. # if __WORDSIZE == 64  
  235. #  define INT64_C(c)    c ## L  
  236. # else  
  237. #  define INT64_C(c)    c ## LL  
  238. # endif  
  239. /* 无符号  */  
  240. # define UINT8_C(c) c  
  241. # define UINT16_C(c)    c  
  242. # define UINT32_C(c)    c ## U  
  243. # if __WORDSIZE == 64  
  244. #  define UINT64_C(c)   c ## UL  
  245. # else  
  246. #  define UINT64_C(c)   c ## ULL  
  247. # endif  
  248. /* 最大类型  */  
  249. # if __WORDSIZE == 64  
  250. #  define INTMAX_C(c)   c ## L  
  251. #  define UINTMAX_C(c)  c ## UL  
  252. # else  
  253. #  define INTMAX_C(c)   c ## LL  
  254. #  define UINTMAX_C(c)  c ## ULL  
  255. # endif  
  256. #endif  /* C++ && constant macros */  
  257. #endif /* stdint.h */  

    解释:
    (1)准确长度类型:具有准确长度,没有填充位,在stdint.h是可选的。类型intN_t, uintN_t,范围INTN_MIN, INTN_MAX, UINTN_MAX,inttypes.h中的控制字符串PRIcN, SCNcN。
    最小长度类型:具有指定长度的最小类型,至少要对N=8,16,32,64定义这些类型。宏INTN_C(constant), UINTN_C(constant)用于把传进来的常数扩展成相应类型的常量(即在后面加常量修饰符,如U,L,LL,ULL)。
    快速长度类型:具有指定最小长度的最快类型。至少要对N=8,16,32,64定义这些类型。
    指针长度类型:intptr_t, uintptr_t,分别是带符号和无符号整数类型,可以放置任何对象指针。范围INTPTR_MIN, INTPTR_MAX, UINTPTR_MAX。
    最大长度类型:intmax_t, uintmax_t,最大带符号和无符号整数类型,所有C语言实现都要定义。宏INTMAX_C(), UINTMAX_C()用于把传进来的常数扩展成相应类型的常量。
    (2)GNU实现中,各种扩展整数类型与我们通常使用的整数类型长度一致,即8位使用char类型,16位使用sort类型,32位使用int类型,64位在64位平台使用long类型,在32位平台上使用long long类型,注意32平台上的long一般和int一样大。只有快速类型有点区别,在32位平台上,16位和32位快速类型均使用int(这样才能快速地按字寻址),64位快速类型使用long long;在64位平台上,16位、32位和64位快速类型均使用long。最大类型与字长度一致,指针类型也与字长度一致。
    (3)有符号整数范围为-2**(N-1)~2**(N-1)-1,其中最小负数-2**(N-1)=100...0没有对应正数,其反数还是自己。无符号整数类型范围为0~2**N-1。
    (4)ptrdiff_t,size_t,wchar_t,wint_t与sig_atomic_t类型的范围也在stdint.h中定义,有PTRDIFF_MIN/PTRDIFF_MAX, SIZE_MAX,  WCHAR_MIN/WCHAR_MAX, WINT_MIN/WINT_MAX, SIG_ATOMIC_MIN/SIG_ATOMIC_MAX。在32位平台的GNU实现中,ptrdiff_t一般为int类型(在64位平台上则为long类型),size_t为unsigned int类型(在64位平台上则为unsigned long类型),wchar_t一般为int类型,wint_t一般为unsigned int类型,sig_atomic_t通常为int类型。
    2、inttypes.h: 定义扩展整数类型的printf和scanf格式控制字符串,用于实现可移植的格式化输出或输入。还定义了一些与stdlib.h中对应的基本算术函数和转换函数。imaxabs和imaxdiv函数类似于stdlib.h中的abs和div函数,imaxabs(x)返回最大类型整数x的绝对值,imaxdiv(n,d)计算最大类型整数的除法n/d,得出的商和余数分别放在imaxdiv_t结构的quot和rem成员中。strtoimax和strtoumax函数将字符串转换成最大类型的整数,与stdlib.h中的strtol和strtoul类似。wcstoimax和wcstoumax函数将宽字符串转换成最大类型的整数,与wchar.h中的wcstol和wcstoul类似。

  1. /* ISO C99: 7.8  整数类型的格式转换   <inttypes.h>  */  
  2. #ifndef _INTTYPES_H  
  3. #define _INTTYPES_H 1  
  4. #include <features.h>  
  5. /* 获取类型定义  */  
  6. #include <stdint.h>  
  7. /* 获取wchar_t的定义,但我们自己不能定义  */  
  8. #ifndef ____gwchar_t_defined  
  9. # ifdef __cplusplus  
  10. #  define __gwchar_t wchar_t  
  11. # elif defined __WCHAR_TYPE__  
  12. typedef __WCHAR_TYPE__ __gwchar_t;  
  13. # else  
  14. #  define __need_wchar_t  
  15. #  include <stddef.h>  
  16. typedef wchar_t __gwchar_t;  
  17. # endif  
  18. # define ____gwchar_t_defined   1  
  19. #endif  
  20. /* ISO C99标准指出,这些宏应该只在被请求到的时候才定义 */  
  21. #if !defined __cplusplus || defined __STDC_FORMAT_MACROS  
  22. # if __WORDSIZE == 64  
  23. #  define __PRI64_PREFIX    "l"  
  24. #  define __PRIPTR_PREFIX   "l"  
  25. # else  
  26. #  define __PRI64_PREFIX    "ll"  
  27. #  define __PRIPTR_PREFIX  
  28. # endif  
  29. /* 表示输出格式控制串的宏  */  
  30. /* 十进制格式  */  
  31. # define PRId8      "d"  /* 表示8位准确类型的输出格式控制串 */  
  32. # define PRId16     "d"  
  33. # define PRId32     "d"  
  34. # define PRId64     __PRI64_PREFIX "d"  
  35. # define PRIdLEAST8 "d"    /* 表示8位最小类型的输出格式控制串 */  
  36. # define PRIdLEAST16    "d"  
  37. # define PRIdLEAST32    "d"  
  38. # define PRIdLEAST64    __PRI64_PREFIX "d"  
  39. # define PRIdFAST8  "d"  
  40. # define PRIdFAST16 __PRIPTR_PREFIX "d"  
  41. # define PRIdFAST32 __PRIPTR_PREFIX "d"  
  42. # define PRIdFAST64 __PRI64_PREFIX "d"  
  43. # define PRIi8      "i"  
  44. # define PRIi16     "i"  
  45. # define PRIi32     "i"  
  46. # define PRIi64     __PRI64_PREFIX "i"  
  47. # define PRIiLEAST8 "i"  
  48. # define PRIiLEAST16    "i"  
  49. # define PRIiLEAST32    "i"  
  50. # define PRIiLEAST64    __PRI64_PREFIX "i"  
  51. # define PRIiFAST8  "i"  
  52. # define PRIiFAST16 __PRIPTR_PREFIX "i"  
  53. # define PRIiFAST32 __PRIPTR_PREFIX "i"  
  54. # define PRIiFAST64 __PRI64_PREFIX "i"  
  55. /* 八进制格式  */  
  56. # define PRIo8      "o"  
  57. # define PRIo16     "o"  
  58. # define PRIo32     "o"  
  59. # define PRIo64     __PRI64_PREFIX "o"  
  60. # define PRIoLEAST8 "o"  
  61. # define PRIoLEAST16    "o"  
  62. # define PRIoLEAST32    "o"  
  63. # define PRIoLEAST64    __PRI64_PREFIX "o"  
  64. # define PRIoFAST8  "o"  
  65. # define PRIoFAST16 __PRIPTR_PREFIX "o"  
  66. # define PRIoFAST32 __PRIPTR_PREFIX "o"  
  67. # define PRIoFAST64 __PRI64_PREFIX "o"  
  68. /* 无符号整数格式  */  
  69. # define PRIu8      "u"  
  70. # define PRIu16     "u"  
  71. # define PRIu32     "u"  
  72. # define PRIu64     __PRI64_PREFIX "u"  
  73. # define PRIuLEAST8 "u"  
  74. # define PRIuLEAST16    "u"  
  75. # define PRIuLEAST32    "u"  
  76. # define PRIuLEAST64    __PRI64_PREFIX "u"  
  77. # define PRIuFAST8  "u"  
  78. # define PRIuFAST16 __PRIPTR_PREFIX "u"  
  79. # define PRIuFAST32 __PRIPTR_PREFIX "u"  
  80. # define PRIuFAST64 __PRI64_PREFIX "u"  
  81. /* 小写十六进制格式  */  
  82. # define PRIx8      "x"  
  83. # define PRIx16     "x"  
  84. # define PRIx32     "x"  
  85. # define PRIx64     __PRI64_PREFIX "x"  
  86. # define PRIxLEAST8 "x"  
  87. # define PRIxLEAST16    "x"  
  88. # define PRIxLEAST32    "x"  
  89. # define PRIxLEAST64    __PRI64_PREFIX "x"  
  90. # define PRIxFAST8  "x"  
  91. # define PRIxFAST16 __PRIPTR_PREFIX "x"  
  92. # define PRIxFAST32 __PRIPTR_PREFIX "x"  
  93. # define PRIxFAST64 __PRI64_PREFIX "x"  
  94. /* 大写十六进制格式  */  
  95. # define PRIX8      "X"  
  96. # define PRIX16     "X"  
  97. # define PRIX32     "X"  
  98. # define PRIX64     __PRI64_PREFIX "X"  
  99. # define PRIXLEAST8 "X"  
  100. # define PRIXLEAST16    "X"  
  101. # define PRIXLEAST32    "X"  
  102. # define PRIXLEAST64    __PRI64_PREFIX "X"  
  103. # define PRIXFAST8  "X"  
  104. # define PRIXFAST16 __PRIPTR_PREFIX "X"  
  105. # define PRIXFAST32 __PRIPTR_PREFIX "X"  
  106. # define PRIXFAST64 __PRI64_PREFIX "X"  
  107. /* 打印intmax_t和uintmax_t的格式宏  */  
  108. # define PRIdMAX    __PRI64_PREFIX "d"  
  109. # define PRIiMAX    __PRI64_PREFIX "i"  
  110. # define PRIoMAX    __PRI64_PREFIX "o"  
  111. # define PRIuMAX    __PRI64_PREFIX "u"  
  112. # define PRIxMAX    __PRI64_PREFIX "x"  
  113. # define PRIXMAX    __PRI64_PREFIX "X"  
  114.   
  115. /* 打印intptr_t和uintptr_t的格式宏  */  
  116. # define PRIdPTR    __PRIPTR_PREFIX "d"  
  117. # define PRIiPTR    __PRIPTR_PREFIX "i"  
  118. # define PRIoPTR    __PRIPTR_PREFIX "o"  
  119. # define PRIuPTR    __PRIPTR_PREFIX "u"  
  120. # define PRIxPTR    __PRIPTR_PREFIX "x"  
  121. # define PRIXPTR    __PRIPTR_PREFIX "X"  
  122. /* 表示输入格式控制串的宏  */  
  123. /* 有符号十进格式  */  
  124. # define SCNd8      "hhd"  
  125. # define SCNd16     "hd"  
  126. # define SCNd32     "d"  
  127. # define SCNd64     __PRI64_PREFIX "d"  
  128. # define SCNdLEAST8 "hhd"  
  129. # define SCNdLEAST16    "hd"  
  130. # define SCNdLEAST32    "d"  
  131. # define SCNdLEAST64    __PRI64_PREFIX "d"  
  132. # define SCNdFAST8  "hhd"  
  133. # define SCNdFAST16 __PRIPTR_PREFIX "d"  
  134. # define SCNdFAST32 __PRIPTR_PREFIX "d"  
  135. # define SCNdFAST64 __PRI64_PREFIX "d"  
  136. /* 有符号十进制格式  */  
  137. # define SCNi8      "hhi"  
  138. # define SCNi16     "hi"  
  139. # define SCNi32     "i"  
  140. # define SCNi64     __PRI64_PREFIX "i"  
  141. # define SCNiLEAST8 "hhi"  
  142. # define SCNiLEAST16    "hi"  
  143. # define SCNiLEAST32    "i"  
  144. # define SCNiLEAST64    __PRI64_PREFIX "i"  
  145. # define SCNiFAST8  "hhi"  
  146. # define SCNiFAST16 __PRIPTR_PREFIX "i"  
  147. # define SCNiFAST32 __PRIPTR_PREFIX "i"  
  148. # define SCNiFAST64 __PRI64_PREFIX "i"  
  149. /* 无符号十进制格式  */  
  150. # define SCNu8      "hhu"  
  151. # define SCNu16     "hu"  
  152. # define SCNu32     "u"  
  153. # define SCNu64     __PRI64_PREFIX "u"  
  154. # define SCNuLEAST8 "hhu"  
  155. # define SCNuLEAST16    "hu"  
  156. # define SCNuLEAST32    "u"  
  157. # define SCNuLEAST64    __PRI64_PREFIX "u"  
  158. # define SCNuFAST8  "hhu"  
  159. # define SCNuFAST16 __PRIPTR_PREFIX "u"  
  160. # define SCNuFAST32 __PRIPTR_PREFIX "u"  
  161. # define SCNuFAST64 __PRI64_PREFIX "u"  
  162. /* 八进制格式  */  
  163. # define SCNo8      "hho"  
  164. # define SCNo16     "ho"  
  165. # define SCNo32     "o"  
  166. # define SCNo64     __PRI64_PREFIX "o"  
  167. # define SCNoLEAST8 "hho"  
  168. # define SCNoLEAST16    "ho"  
  169. # define SCNoLEAST32    "o"  
  170. # define SCNoLEAST64    __PRI64_PREFIX "o"  
  171. # define SCNoFAST8  "hho"  
  172. # define SCNoFAST16 __PRIPTR_PREFIX "o"  
  173. # define SCNoFAST32 __PRIPTR_PREFIX "o"  
  174. # define SCNoFAST64 __PRI64_PREFIX "o"  
  175. /* 十六进制格式  */  
  176. # define SCNx8      "hhx"  
  177. # define SCNx16     "hx"  
  178. # define SCNx32     "x"  
  179. # define SCNx64     __PRI64_PREFIX "x"  
  180. # define SCNxLEAST8 "hhx"  
  181. # define SCNxLEAST16    "hx"  
  182. # define SCNxLEAST32    "x"  
  183. # define SCNxLEAST64    __PRI64_PREFIX "x"  
  184. # define SCNxFAST8  "hhx"  
  185. # define SCNxFAST16 __PRIPTR_PREFIX "x"  
  186. # define SCNxFAST32 __PRIPTR_PREFIX "x"  
  187. # define SCNxFAST64 __PRI64_PREFIX "x"  
  188.   
  189. /* 输入intmax_t和uintmax_t的格式宏  */  
  190. # define SCNdMAX    __PRI64_PREFIX "d"  
  191. # define SCNiMAX    __PRI64_PREFIX "i"  
  192. # define SCNoMAX    __PRI64_PREFIX "o"  
  193. # define SCNuMAX    __PRI64_PREFIX "u"  
  194. # define SCNxMAX    __PRI64_PREFIX "x"  
  195. /* 输入intptr_t和uintptr_t的格式宏  */  
  196. # define SCNdPTR    __PRIPTR_PREFIX "d"  
  197. # define SCNiPTR    __PRIPTR_PREFIX "i"  
  198. # define SCNoPTR    __PRIPTR_PREFIX "o"  
  199. # define SCNuPTR    __PRIPTR_PREFIX "u"  
  200. # define SCNxPTR    __PRIPTR_PREFIX "x"  
  201. #endif  /* C++ && format macros */  
  202.   
  203. __BEGIN_DECLS  
  204. #if __WORDSIZE == 64  
  205. /* 64位平台 */  
  206. /* 我们要使用ldiv_t来定义uintmax_t  */  
  207. typedef struct  
  208.   {  
  209.     long int quot;      /* 商  */  
  210.     long int rem;       /* 余数  */  
  211.   } imaxdiv_t;  
  212. #else   /*  32位平台 */  
  213. /* 我们要使用lldiv_t来定义uintmax_t  */  
  214. typedef struct  
  215.   {  
  216.     long long int quot;     /* 商  */  
  217.     long long int rem;      /* 余数  */  
  218.   } imaxdiv_t;  
  219. #endif  
  220. /* 计算N的绝对值  */  
  221. extern intmax_t imaxabs (intmax_t __n) __THROW __attribute__ ((__const__));  
  222. /* 返回NUMER除以DENOM的商和余数,放在imaxdiv_t结构中 */  
  223. extern imaxdiv_t imaxdiv (intmax_t __numer, intmax_t __denom)  
  224.       __THROW __attribute__ ((__const__));  
  225. /* 与strtol类似,但转换成intmax_t  */  
  226. extern intmax_t strtoimax (__const char *__restrict __nptr,  
  227.                char **__restrict __endptr, int __base) __THROW;  
  228. /* 与strtoul类似,但转换成uintmax_t  */  
  229. extern uintmax_t strtoumax (__const char *__restrict __nptr,  
  230.                 char ** __restrict __endptr, int __base) __THROW;  
  231. /* 与wcstol类似,但转换成intmax_t  */  
  232. extern intmax_t wcstoimax (__const __gwchar_t *__restrict __nptr,  
  233.                __gwchar_t **__restrict __endptr, int __base)  
  234.      __THROW;  
  235. /* 与wcstoul类似,但转换成uintmax_t  */  
  236. extern uintmax_t wcstoumax (__const __gwchar_t *__restrict __nptr,  
  237.                 __gwchar_t ** __restrict __endptr, int __base)  
  238.      __THROW;  
  239.        
  240. /* 下面是GNU的扩展:是上面4个标准转换函数的外部内联版本,有64位平台和32位平台之分 */  
  241. __END_DECLS  
  242. #endif /* inttypes.h */  

    注意对于inttypes.h和stdlib.h中的绝对值函数和除法运算函数,很容易实现,编译器通常把它们当作内部函数(标准C中允许这样做),因此在glibc中并没有这些函数的源代码。
    3、strtoimax和strtoumax函数: 将字符串转换成intmax_t类型的整数,一个为有符号,一个为无符号。其实现都是调用内部函数来完成的。

  1. /* strtoimax.c:将字符串转换成最大整数类型  */  
  2. #include <inttypes.h>  
  3. #include <stdlib.h>  
  4. intmax_t  
  5. strtoimax (const char *__restrict nptr, char **__restrict endptr, int base)  
  6. {  
  7.   return __strtoll_internal (nptr, endptr, base, 0);  
  8. }  

  1. /* strtoumax.c:将字符串转换成无符号的最大整数类型  */  
  2. #include <inttypes.h>  
  3. #include <stdlib.h>  
  4. uintmax_t  
  5. strtoumax (const char *__restrict nptr, char **__restrict endptr, int base)  
  6. {  
  7.   return __strtoull_internal (nptr, endptr, base, 0);  
  8. }  

    4、wcstoimax和wcstoumax函数: 将宽字符串转换成intmax_t类型的整数,一个为有符号,一个为无符号。其实现也都是调用内部函数来完成的。

  1. /* wcstoimax.c:将宽字符串转换成最大整数类型  */  
  2. #include <inttypes.h>  
  3. #include <wchar.h>  
  4. intmax_t  
  5. wcstoimax (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,  
  6.        int base)  
  7. {  
  8.   return __wcstoll_internal (nptr, endptr, base, 0);  
  9. }  

  1. /* wcstoumax.c:将宽字符串转换成无符号的最大整数类型  */  
  2. #include <inttypes.h>  
  3. #include <wchar.h>  
  4. uintmax_t  
  5. wcstoumax (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,  
  6.        int base)  
  7. {  
  8.   return __wcstoull_internal (nptr, endptr, base, 0);  
  9. }  

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值