固定二进制位的整型变量

C99中,设置了stdint.h来定义一组整型数据类型,形如:intN_t和uintN_t对不同的N值指定N位有符号和无符号整数,N的值一般为:8,16,32,64。这样,我们就可以无歧义的声明一个16位无符号变量:uint16_t  a

如果要想用printf打印这样声明的变量,可移植的做法是,包含头文件inttypes.h(它内部包含了stdint.h),该头文件中定义了一串类似PRId32,PRId64,PRIu32,PRIu64等等的宏,根据系统的不同扩展为不同的含义。

###inttypes.h头文件片段
 42 
 43 # if __WORDSIZE == 64
 44 #  define __PRI64_PREFIX    "l"
 45 #  define __PRIPTR_PREFIX   "l"
 46 # else
 47 #  define __PRI64_PREFIX    "ll"
 48 #  define __PRIPTR_PREFIX
 49 # endif
 50 
 51 /* Macros for printing format specifiers.  */
 52 
 53 /* Decimal notation.  */
 54 # define PRId8      "d"
 55 # define PRId16     "d"
 56 # define PRId32     "d"
 57 # define PRId64     __PRI64_PREFIX "d"
 58 
101 /* Unsigned integers.  */
102 # define PRIu8      "u"
103 # define PRIu16     "u"
104 # define PRIu32     "u"
105 # define PRIu64     __PRI64_PREFIX "u"
  1 #include <stdio.h>                                                          
  2 #include <inttypes.h>
  3 int main(int argc, char *argv[])
  4 {
  5     int32_t t=10;
  6     uint64_t t1=200;
  7     printf("t=%"PRId32",t1=%"PRIu64"
",t,t1);  //注意宏在双引号外边
  8 
  9     return 0;
 10 }
原文地址:https://www.cnblogs.com/litifeng/p/8445958.html