您的位置:首页 > 其它

扩展整数类型stdint.h和inttypes.h

2014-06-06 20:35 801 查看
原文出自:http://blog.csdn.net/zhoudaxia/article/details/4704766

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是该类型的宽度。

[cpp]
view plaincopy

/* ISO C99: 7.18 整数类型 <stdint.h>  */  
#ifndef _STDINT_H  
#define _STDINT_H   1  
#include <features.h>  
#include <bits/wchar.h>  
#include <bits/wordsize.h>  
/* 准确类型:指定长度的准确类型  */  
/* 有符号  */  
/* 有一些与<sys/types.h>中众所周知的inet代码重叠 */  
#ifndef __int8_t_defined  
# define __int8_t_defined  
typedef signed char     int8_t; /* 8位的准确长度类型int8_t=signed char */  
typedef short int       int16_t; /* 16位的准确长度类型 */  
typedef int         int32_t;  
# if __WORDSIZE == 64   
typedef long int        int64_t;  /*  64位平台上64位的准确类型为long */  
# else  
__extension__  
typedef long long int       int64_t; /*  32位平台上64位的准确类型为long long */  
# endif  
#endif  
/* 无符号  */  
typedef unsigned char       uint8_t;  
typedef unsigned short int  uint16_t;  
#ifndef __uint32_t_defined  
typedef unsigned int        uint32_t;  
# define __uint32_t_defined  
#endif  
#if __WORDSIZE == 64  
typedef unsigned long int   uint64_t;  
#else  
__extension__  
typedef unsigned long long int  uint64_t;  
#endif  
  
/* 最小类型:指定长度的最小类型  */  
/* 有符号  */  
typedef signed char     int_least8_t;  
typedef short int       int_least16_t;  
typedef int         int_least32_t;  
#if __WORDSIZE == 64  
typedef long int        int_least64_t;  
#else  
__extension__  
typedef long long int       int_least64_t;  
#endif  
/* 无符号  */  
typedef unsigned char       uint_least8_t;  
typedef unsigned short int  uint_least16_t;  
typedef unsigned int        uint_least32_t;  
#if __WORDSIZE == 64  
typedef unsigned long int   uint_least64_t;  
#else  
__extension__  
typedef unsigned long long int  uint_least64_t;  
#endif  
  
/* 快速类型:指定长度的最快类型  */  
/* 有符号  */  
typedef signed char     int_fast8_t;  
#if __WORDSIZE == 64       /* 64位平台 */  
typedef long int        int_fast16_t;  
typedef long int        int_fast32_t;  
typedef long int        int_fast64_t;  
#else     /* 32位平台 */  
typedef int         int_fast16_t; /* 16位和32位的最快类型均为int */  
typedef int         int_fast32_t;  
__extension__  
typedef long long int       int_fast64_t;  
#endif  
/* 无符号  */  
typedef unsigned char       uint_fast8_t;  
#if __WORDSIZE == 64  
typedef unsigned long int   uint_fast16_t;  
typedef unsigned long int   uint_fast32_t;  
typedef unsigned long int   uint_fast64_t;  
#else  
typedef unsigned int        uint_fast16_t;  
typedef unsigned int        uint_fast32_t;  
__extension__  
typedef unsigned long long int  uint_fast64_t;  
#endif  
  
/* 通用指针类型:即void * 型指针的类型,64位平台上为long,32位平台上为int  */  
#if __WORDSIZE == 64  
# ifndef __intptr_t_defined  
typedef long int        intptr_t;  
#  define __intptr_t_defined  
# endif  
typedef unsigned long int   uintptr_t;  
#else  
# ifndef __intptr_t_defined  
typedef int         intptr_t;  
#  define __intptr_t_defined  
# endif  
typedef unsigned int        uintptr_t;  
#endif  
  
/* 最大类型:指定长度的最大整数类型  */  
#if __WORDSIZE == 64  
typedef long int        intmax_t;  /* 64位平台上的最大整数类型为long */  
typedef unsigned long int   uintmax_t;  
#else  
__extension__  
typedef long long int       intmax_t;  /* 32位平台上的最大整数类型为long long */  
__extension__  
typedef unsigned long long int  uintmax_t;  
#endif  
/* ISO C99标准指出,在C++实现中这些宏应该只在被请求到的时候才定义 */  
#if !defined __cplusplus || defined __STDC_LIMIT_MACROS  
# if __WORDSIZE == 64  
#  define __INT64_C(c)  c ## L  
#  define __UINT64_C(c) c ## UL  
# else  
#  define __INT64_C(c)  c ## LL  
#  define __UINT64_C(c) c ## ULL  
# endif  
/* 整数类型的范围  */  
/* 有符号整数类型的最小值:-2**(N-1),其中最小负数-2**(N-1)=100...0没有对应正数,其反数还是自己  */  
# define INT8_MIN       (-128)  
# define INT16_MIN      (-32767-1)  
# define INT32_MIN      (-2147483647-1)  
# define INT64_MIN      (-__INT64_C(9223372036854775807)-1)  
/* 有符号整数类型的最大值:2**(N-1)-1  */  
# define INT8_MAX       (127)  
# define INT16_MAX      (32767)  
# define INT32_MAX      (2147483647)  
# define INT64_MAX      (__INT64_C(9223372036854775807))  
/* 无符号整数类型的最大值:2**(N-1)-1,注意有Min=-MAX-1  */  
# define UINT8_MAX      (255)  
# define UINT16_MAX     (65535)  
# define UINT32_MAX     (4294967295U)  
# define UINT64_MAX     (__UINT64_C(18446744073709551615))  
  
/* 有符号最小类型的最小值  */  
# define INT_LEAST8_MIN     (-128)  
# define INT_LEAST16_MIN    (-32767-1)  
# define INT_LEAST32_MIN    (-2147483647-1)  
# define INT_LEAST64_MIN    (-__INT64_C(9223372036854775807)-1)  
/* 有符号最小类型的最大值  */  
# define INT_LEAST8_MAX     (127)  
# define INT_LEAST16_MAX    (32767)  
# define INT_LEAST32_MAX    (2147483647)  
# define INT_LEAST64_MAX    (__INT64_C(9223372036854775807))  
/* 无符号最小类型的最大值  */  
# define UINT_LEAST8_MAX    (255)  
# define UINT_LEAST16_MAX   (65535)  
# define UINT_LEAST32_MAX   (4294967295U)  
# define UINT_LEAST64_MAX   (__UINT64_C(18446744073709551615))  
  
/* 有符号快速类型的最小值  */  
# define INT_FAST8_MIN      (-128)  
# if __WORDSIZE == 64  
#  define INT_FAST16_MIN    (-9223372036854775807L-1)  
#  define INT_FAST32_MIN    (-9223372036854775807L-1)  
# else  
#  define INT_FAST16_MIN    (-2147483647-1)  
#  define INT_FAST32_MIN    (-2147483647-1)  
# endif  
# define INT_FAST64_MIN     (-__INT64_C(9223372036854775807)-1)  
/* 有符号快速类型的最大值  */  
# define INT_FAST8_MAX      (127)  
# if __WORDSIZE == 64  
#  define INT_FAST16_MAX    (9223372036854775807L)  
#  define INT_FAST32_MAX    (9223372036854775807L)  
# else  
#  define INT_FAST16_MAX    (2147483647)  
#  define INT_FAST32_MAX    (2147483647)  
# endif  
# define INT_FAST64_MAX     (__INT64_C(9223372036854775807))  
/* 无符号快速类型的最大值  */  
# define UINT_FAST8_MAX     (255)  
# if __WORDSIZE == 64  
#  define UINT_FAST16_MAX   (18446744073709551615UL)  
#  define UINT_FAST32_MAX   (18446744073709551615UL)  
# else  
#  define UINT_FAST16_MAX   (4294967295U)  
#  define UINT_FAST32_MAX   (4294967295U)  
# endif  
# define UINT_FAST64_MAX    (__UINT64_C(18446744073709551615))  
  
/* 指针类型(持有void*型指针)的范围  */  
# if __WORDSIZE == 64  
#  define INTPTR_MIN        (-9223372036854775807L-1)  
#  define INTPTR_MAX        (9223372036854775807L)  
#  define UINTPTR_MAX       (18446744073709551615UL)  
# else  
#  define INTPTR_MIN        (-2147483647-1)  
#  define INTPTR_MAX        (2147483647)  
#  define UINTPTR_MAX       (4294967295U)  
# endif  
  
/* 有符号最大类型的最小值  */  
# define INTMAX_MIN     (-__INT64_C(9223372036854775807)-1)  
/* 符号最大类型的最大值  */  
# define INTMAX_MAX     (__INT64_C(9223372036854775807))  
/* 无符号最大类型的最大值 */  
# define UINTMAX_MAX        (__UINT64_C(18446744073709551615))  
  
/* 其他整数类型的范围  */  
/* ptrdiff_t类型的范围  */  
# if __WORDSIZE == 64  
#  define PTRDIFF_MIN       (-9223372036854775807L-1)  
#  define PTRDIFF_MAX       (9223372036854775807L)  
# else  
#  define PTRDIFF_MIN       (-2147483647-1)  
#  define PTRDIFF_MAX       (2147483647)  
# endif  
/* sig_atomic_t类型的范围  */  
# define SIG_ATOMIC_MIN     (-2147483647-1)  
# define SIG_ATOMIC_MAX     (2147483647)  
/* size_t类型的范围  */  
# if __WORDSIZE == 64  
#  define SIZE_MAX      (18446744073709551615UL)  
# else  
#  define SIZE_MAX      (4294967295U)  
# endif  
/* wchar_t类型的范围  */  
# ifndef WCHAR_MIN  
/* 这些常量可能在<wchar.h>也定义了  */  
#  define WCHAR_MIN     __WCHAR_MIN  
#  define WCHAR_MAX     __WCHAR_MAX  
# endif  
/* wint_t类型的范围  */  
# define WINT_MIN       (0u)  
# define WINT_MAX       (4294967295u)  
#endif  /* C++ && limit macros */  
/* ISO C99标准指出,在C++实现中这些宏应该只在被请求到的时候才定义 */  
#if !defined __cplusplus || defined __STDC_CONSTANT_MACROS  
/* 有符号  */  
# define INT8_C(c)  c  
# define INT16_C(c) c  
# define INT32_C(c) c  
# if __WORDSIZE == 64  
#  define INT64_C(c)    c ## L  
# else  
#  define INT64_C(c)    c ## LL  
# endif  
/* 无符号  */  
# define UINT8_C(c) c  
# define UINT16_C(c)    c  
# define UINT32_C(c)    c ## U  
# if __WORDSIZE == 64  
#  define UINT64_C(c)   c ## UL  
# else  
#  define UINT64_C(c)   c ## ULL  
# endif  
/* 最大类型  */  
# if __WORDSIZE == 64  
#  define INTMAX_C(c)   c ## L  
#  define UINTMAX_C(c)  c ## UL  
# else  
#  define INTMAX_C(c)   c ## LL  
#  define UINTMAX_C(c)  c ## ULL  
# endif  
#endif  /* C++ && constant macros */  
#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类似。

[cpp]
view plaincopy

/* ISO C99: 7.8  整数类型的格式转换   <inttypes.h>  */  
#ifndef _INTTYPES_H  
#define _INTTYPES_H 1  
#include <features.h>  
/* 获取类型定义  */  
#include <stdint.h>  
/* 获取wchar_t的定义,但我们自己不能定义  */  
#ifndef ____gwchar_t_defined  
# ifdef __cplusplus  
#  define __gwchar_t wchar_t  
# elif defined __WCHAR_TYPE__  
typedef __WCHAR_TYPE__ __gwchar_t;  
# else  
#  define __need_wchar_t  
#  include <stddef.h>  
typedef wchar_t __gwchar_t;  
# endif  
# define ____gwchar_t_defined   1  
#endif  
/* ISO C99标准指出,这些宏应该只在被请求到的时候才定义 */  
#if !defined __cplusplus || defined __STDC_FORMAT_MACROS  
# if __WORDSIZE == 64  
#  define __PRI64_PREFIX    "l"  
#  define __PRIPTR_PREFIX   "l"  
# else  
#  define __PRI64_PREFIX    "ll"  
#  define __PRIPTR_PREFIX  
# endif  
/* 表示输出格式控制串的宏  */  
/* 十进制格式  */  
# define PRId8      "d"  /* 表示8位准确类型的输出格式控制串 */  
# define PRId16     "d"  
# define PRId32     "d"  
# define PRId64     __PRI64_PREFIX "d"  
# define PRIdLEAST8 "d"    /* 表示8位最小类型的输出格式控制串 */  
# define PRIdLEAST16    "d"  
# define PRIdLEAST32    "d"  
# define PRIdLEAST64    __PRI64_PREFIX "d"  
# define PRIdFAST8  "d"  
# define PRIdFAST16 __PRIPTR_PREFIX "d"  
# define PRIdFAST32 __PRIPTR_PREFIX "d"  
# define PRIdFAST64 __PRI64_PREFIX "d"  
# define PRIi8      "i"  
# define PRIi16     "i"  
# define PRIi32     "i"  
# define PRIi64     __PRI64_PREFIX "i"  
# define PRIiLEAST8 "i"  
# define PRIiLEAST16    "i"  
# define PRIiLEAST32    "i"  
# define PRIiLEAST64    __PRI64_PREFIX "i"  
# define PRIiFAST8  "i"  
# define PRIiFAST16 __PRIPTR_PREFIX "i"  
# define PRIiFAST32 __PRIPTR_PREFIX "i"  
# define PRIiFAST64 __PRI64_PREFIX "i"  
/* 八进制格式  */  
# define PRIo8      "o"  
# define PRIo16     "o"  
# define PRIo32     "o"  
# define PRIo64     __PRI64_PREFIX "o"  
# define PRIoLEAST8 "o"  
# define PRIoLEAST16    "o"  
# define PRIoLEAST32    "o"  
# define PRIoLEAST64    __PRI64_PREFIX "o"  
# define PRIoFAST8  "o"  
# define PRIoFAST16 __PRIPTR_PREFIX "o"  
# define PRIoFAST32 __PRIPTR_PREFIX "o"  
# define PRIoFAST64 __PRI64_PREFIX "o"  
/* 无符号整数格式  */  
# define PRIu8      "u"  
# define PRIu16     "u"  
# define PRIu32     "u"  
# define PRIu64     __PRI64_PREFIX "u"  
# define PRIuLEAST8 "u"  
# define PRIuLEAST16    "u"  
# define PRIuLEAST32    "u"  
# define PRIuLEAST64    __PRI64_PREFIX "u"  
# define PRIuFAST8  "u"  
# define PRIuFAST16 __PRIPTR_PREFIX "u"  
# define PRIuFAST32 __PRIPTR_PREFIX "u"  
# define PRIuFAST64 __PRI64_PREFIX "u"  
/* 小写十六进制格式  */  
# define PRIx8      "x"  
# define PRIx16     "x"  
# define PRIx32     "x"  
# define PRIx64     __PRI64_PREFIX "x"  
# define PRIxLEAST8 "x"  
# define PRIxLEAST16    "x"  
# define PRIxLEAST32    "x"  
# define PRIxLEAST64    __PRI64_PREFIX "x"  
# define PRIxFAST8  "x"  
# define PRIxFAST16 __PRIPTR_PREFIX "x"  
# define PRIxFAST32 __PRIPTR_PREFIX "x"  
# define PRIxFAST64 __PRI64_PREFIX "x"  
/* 大写十六进制格式  */  
# define PRIX8      "X"  
# define PRIX16     "X"  
# define PRIX32     "X"  
# define PRIX64     __PRI64_PREFIX "X"  
# define PRIXLEAST8 "X"  
# define PRIXLEAST16    "X"  
# define PRIXLEAST32    "X"  
# define PRIXLEAST64    __PRI64_PREFIX "X"  
# define PRIXFAST8  "X"  
# define PRIXFAST16 __PRIPTR_PREFIX "X"  
# define PRIXFAST32 __PRIPTR_PREFIX "X"  
# define PRIXFAST64 __PRI64_PREFIX "X"  
/* 打印intmax_t和uintmax_t的格式宏  */  
# define PRIdMAX    __PRI64_PREFIX "d"  
# define PRIiMAX    __PRI64_PREFIX "i"  
# define PRIoMAX    __PRI64_PREFIX "o"  
# define PRIuMAX    __PRI64_PREFIX "u"  
# define PRIxMAX    __PRI64_PREFIX "x"  
# define PRIXMAX    __PRI64_PREFIX "X"  
  
/* 打印intptr_t和uintptr_t的格式宏  */  
# define PRIdPTR    __PRIPTR_PREFIX "d"  
# define PRIiPTR    __PRIPTR_PREFIX "i"  
# define PRIoPTR    __PRIPTR_PREFIX "o"  
# define PRIuPTR    __PRIPTR_PREFIX "u"  
# define PRIxPTR    __PRIPTR_PREFIX "x"  
# define PRIXPTR    __PRIPTR_PREFIX "X"  
/* 表示输入格式控制串的宏  */  
/* 有符号十进格式  */  
# define SCNd8      "hhd"  
# define SCNd16     "hd"  
# define SCNd32     "d"  
# define SCNd64     __PRI64_PREFIX "d"  
# define SCNdLEAST8 "hhd"  
# define SCNdLEAST16    "hd"  
# define SCNdLEAST32    "d"  
# define SCNdLEAST64    __PRI64_PREFIX "d"  
# define SCNdFAST8  "hhd"  
# define SCNdFAST16 __PRIPTR_PREFIX "d"  
# define SCNdFAST32 __PRIPTR_PREFIX "d"  
# define SCNdFAST64 __PRI64_PREFIX "d"  
/* 有符号十进制格式  */  
# define SCNi8      "hhi"  
# define SCNi16     "hi"  
# define SCNi32     "i"  
# define SCNi64     __PRI64_PREFIX "i"  
# define SCNiLEAST8 "hhi"  
# define SCNiLEAST16    "hi"  
# define SCNiLEAST32    "i"  
# define SCNiLEAST64    __PRI64_PREFIX "i"  
# define SCNiFAST8  "hhi"  
# define SCNiFAST16 __PRIPTR_PREFIX "i"  
# define SCNiFAST32 __PRIPTR_PREFIX "i"  
# define SCNiFAST64 __PRI64_PREFIX "i"  
/* 无符号十进制格式  */  
# define SCNu8      "hhu"  
# define SCNu16     "hu"  
# define SCNu32     "u"  
# define SCNu64     __PRI64_PREFIX "u"  
# define SCNuLEAST8 "hhu"  
# define SCNuLEAST16    "hu"  
# define SCNuLEAST32    "u"  
# define SCNuLEAST64    __PRI64_PREFIX "u"  
# define SCNuFAST8  "hhu"  
# define SCNuFAST16 __PRIPTR_PREFIX "u"  
# define SCNuFAST32 __PRIPTR_PREFIX "u"  
# define SCNuFAST64 __PRI64_PREFIX "u"  
/* 八进制格式  */  
# define SCNo8      "hho"  
# define SCNo16     "ho"  
# define SCNo32     "o"  
# define SCNo64     __PRI64_PREFIX "o"  
# define SCNoLEAST8 "hho"  
# define SCNoLEAST16    "ho"  
# define SCNoLEAST32    "o"  
# define SCNoLEAST64    __PRI64_PREFIX "o"  
# define SCNoFAST8  "hho"  
# define SCNoFAST16 __PRIPTR_PREFIX "o"  
# define SCNoFAST32 __PRIPTR_PREFIX "o"  
# define SCNoFAST64 __PRI64_PREFIX "o"  
/* 十六进制格式  */  
# define SCNx8      "hhx"  
# define SCNx16     "hx"  
# define SCNx32     "x"  
# define SCNx64     __PRI64_PREFIX "x"  
# define SCNxLEAST8 "hhx"  
# define SCNxLEAST16    "hx"  
# define SCNxLEAST32    "x"  
# define SCNxLEAST64    __PRI64_PREFIX "x"  
# define SCNxFAST8  "hhx"  
# define SCNxFAST16 __PRIPTR_PREFIX "x"  
# define SCNxFAST32 __PRIPTR_PREFIX "x"  
# define SCNxFAST64 __PRI64_PREFIX "x"  
  
/* 输入intmax_t和uintmax_t的格式宏  */  
# define SCNdMAX    __PRI64_PREFIX "d"  
# define SCNiMAX    __PRI64_PREFIX "i"  
# define SCNoMAX    __PRI64_PREFIX "o"  
# define SCNuMAX    __PRI64_PREFIX "u"  
# define SCNxMAX    __PRI64_PREFIX "x"  
/* 输入intptr_t和uintptr_t的格式宏  */  
# define SCNdPTR    __PRIPTR_PREFIX "d"  
# define SCNiPTR    __PRIPTR_PREFIX "i"  
# define SCNoPTR    __PRIPTR_PREFIX "o"  
# define SCNuPTR    __PRIPTR_PREFIX "u"  
# define SCNxPTR    __PRIPTR_PREFIX "x"  
#endif  /* C++ && format macros */  
  
__BEGIN_DECLS  
#if __WORDSIZE == 64  
/* 64位平台 */  
/* 我们要使用ldiv_t来定义uintmax_t  */  
typedef struct  
  {  
    long int quot;      /* 商  */  
    long int rem;       /* 余数  */  
  } imaxdiv_t;  
#else   /*  32位平台 */  
/* 我们要使用lldiv_t来定义uintmax_t  */  
typedef struct  
  {  
    long long int quot;     /* 商  */  
    long long int rem;      /* 余数  */  
  } imaxdiv_t;  
#endif  
/* 计算N的绝对值  */  
extern intmax_t imaxabs (intmax_t __n) __THROW __attribute__ ((__const__));  
/* 返回NUMER除以DENOM的商和余数,放在imaxdiv_t结构中 */  
extern imaxdiv_t imaxdiv (intmax_t __numer, intmax_t __denom)  
      __THROW __attribute__ ((__const__));  
/* 与strtol类似,但转换成intmax_t  */  
extern intmax_t strtoimax (__const char *__restrict __nptr,  
               char **__restrict __endptr, int __base) __THROW;  
/* 与strtoul类似,但转换成uintmax_t  */  
extern uintmax_t strtoumax (__const char *__restrict __nptr,  
                char ** __restrict __endptr, int __base) __THROW;  
/* 与wcstol类似,但转换成intmax_t  */  
extern intmax_t wcstoimax (__const __gwchar_t *__restrict __nptr,  
               __gwchar_t **__restrict __endptr, int __base)  
     __THROW;  
/* 与wcstoul类似,但转换成uintmax_t  */  
extern uintmax_t wcstoumax (__const __gwchar_t *__restrict __nptr,  
                __gwchar_t ** __restrict __endptr, int __base)  
     __THROW;  
       
/* 下面是GNU的扩展:是上面4个标准转换函数的外部内联版本,有64位平台和32位平台之分 */  
__END_DECLS  
#endif /* inttypes.h */  

    注意对于inttypes.h和stdlib.h中的绝对值函数和除法运算函数,很容易实现,编译器通常把它们当作内部函数(标准C中允许这样做),因此在glibc中并没有这些函数的源代码。

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

[cpp]
view plaincopy

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

[cpp]
view plaincopy

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

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

[cpp]
view plaincopy

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

[cpp]
view plaincopy

/* wcstoumax.c:将宽字符串转换成无符号的最大整数类型  */  
#include <inttypes.h>  
#include <wchar.h>  
uintmax_t  
wcstoumax (const wchar_t *__restrict nptr, wchar_t **__restrict endptr,  
       int base)  
{  
  return __wcstoull_internal (nptr, endptr, base, 0);  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐