C语言拾遗——inttypes.h

时间:2020-01-09
本文章向大家介绍C语言拾遗——inttypes.h,主要包括C语言拾遗——inttypes.h使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

  今天偶然间看到这个头文件inttypes,好奇有什么用,去找度娘玩了一波,发现这头文件挺有意思的。

  这个头文件适配于C99标准,它提供整数输入的各种进制转换的宏,这是在Ubuntu上扣下来的代码(windows里我没找到放在哪)嗯~ o(* ̄▽ ̄*)o我拿VScode找到了……所以划掉

  不完整的

Ubuntu:

  1 //这里应该是10进制的宏
  2 /* Decimal notation.  */
  3 # define PRId8      "d"
  4 # define PRId16     "d"
  5 # define PRId32     "d"
  6 # define PRId64     __PRI64_PREFIX "d"
  7 
  8 # define PRIdLEAST8 "d"
  9 # define PRIdLEAST16    "d"
 10 # define PRIdLEAST32    "d"
 11 # define PRIdLEAST64    __PRI64_PREFIX "d"
 12 
 13 # define PRIdFAST8  "d"
 14 # define PRIdFAST16 __PRIPTR_PREFIX "d"
 15 # define PRIdFAST32 __PRIPTR_PREFIX "d"
 16 # define PRIdFAST64 __PRI64_PREFIX "d"
 17 
 18 
 19 # define PRIi8      "i"
 20 # define PRIi16     "i"
 21 # define PRIi32     "i"
 22 # define PRIi64     __PRI64_PREFIX "i"
 23 
 24 # define PRIiLEAST8 "i"
 25 # define PRIiLEAST16    "i"
 26 # define PRIiLEAST32    "i"
 27 # define PRIiLEAST64    __PRI64_PREFIX "i"
 28 
 29 # define PRIiFAST8  "i"
 30 # define PRIiFAST16 __PRIPTR_PREFIX "i"
 31 # define PRIiFAST32 __PRIPTR_PREFIX "i"
 32 # define PRIiFAST64 __PRI64_PREFIX "i"
 33 
 34 //感觉这里是8进制的宏
 35 /* Octal notation.  */
 36 # define PRIo8      "o"
 37 # define PRIo16     "o"
 38 # define PRIo32     "o"
 39 # define PRIo64     __PRI64_PREFIX "o"
 40 
 41 # define PRIoLEAST8 "o"
 42 # define PRIoLEAST16    "o"
 43 # define PRIoLEAST32    "o"
 44 # define PRIoLEAST64    __PRI64_PREFIX "o"
 45 
 46 # define PRIoFAST8  "o"
 47 # define PRIoFAST16 __PRIPTR_PREFIX "o"
 48 # define PRIoFAST32 __PRIPTR_PREFIX "o"
 49 # define PRIoFAST64 __PRI64_PREFIX "o"
 50 
 51 //这里因该是无符号的宏
 52 /* Unsigned integers.  */
 53 # define PRIu8      "u"
 54 # define PRIu16     "u"
 55 # define PRIu32     "u"
 56 # define PRIu64     __PRI64_PREFIX "u"
 57 
 58 # define PRIuLEAST8 "u"
 59 # define PRIuLEAST16    "u"
 60 # define PRIuLEAST32    "u"
 61 # define PRIuLEAST64    __PRI64_PREFIX "u"
 62 
 63 # define PRIuFAST8  "u"
 64 # define PRIuFAST16 __PRIPTR_PREFIX "u"
 65 # define PRIuFAST32 __PRIPTR_PREFIX "u"
 66 # define PRIuFAST64 __PRI64_PREFIX "u"
 67 
 68 //这里因该是小写16进制
 69 /* lowercase hexadecimal notation.  */
 70 # define PRIx8      "x"
 71 # define PRIx16     "x"
 72 # define PRIx32     "x"
 73 # define PRIx64     __PRI64_PREFIX "x"
 74 
 75 # define PRIxLEAST8 "x"
 76 # define PRIxLEAST16    "x"
 77 # define PRIxLEAST32    "x"
 78 # define PRIxLEAST64    __PRI64_PREFIX "x"
 79 
 80 # define PRIxFAST8  "x"
 81 # define PRIxFAST16 __PRIPTR_PREFIX "x"
 82 # define PRIxFAST32 __PRIPTR_PREFIX "x"
 83 # define PRIxFAST64 __PRI64_PREFIX "x"
 84 
 85 //这里因该是大写16进制
 86 /* UPPERCASE hexadecimal notation.  */
 87 # define PRIX8      "X"
 88 # define PRIX16     "X"
 89 # define PRIX32     "X"
 90 # define PRIX64     __PRI64_PREFIX "X"
 91 
 92 # define PRIXLEAST8 "X"
 93 # define PRIXLEAST16    "X"
 94 # define PRIXLEAST32    "X"
 95 # define PRIXLEAST64    __PRI64_PREFIX "X"
 96 
 97 # define PRIXFAST8  "X"
 98 # define PRIXFAST16 __PRIPTR_PREFIX "X"
 99 # define PRIXFAST32 __PRIPTR_PREFIX "X"
100 # define PRIXFAST64 __PRI64_PREFIX "X"

Windows:

/* fprintf macros for signed types */
#define PRId8 "d"
#define PRId16 "d"
#define PRId32 "d"
#define PRId64 "I64d"

#define PRIdLEAST8 "d"
#define PRIdLEAST16 "d"
#define PRIdLEAST32 "d"
#define PRIdLEAST64 "I64d"

#define PRIdFAST8 "d"
#define PRIdFAST16 "d"
#define PRIdFAST32 "d"
#define PRIdFAST64 "I64d"

#define PRIdMAX "I64d"
#define PRIdPTR "d"

#define PRIi8 "i"
#define PRIi16 "i"
#define PRIi32 "i"
#define PRIi64 "I64i"

#define PRIiLEAST8 "i"
#define PRIiLEAST16 "i"
#define PRIiLEAST32 "i"
#define PRIiLEAST64 "I64i"

#define PRIiFAST8 "i"
#define PRIiFAST16 "i"
#define PRIiFAST32 "i"
#define PRIiFAST64 "I64i"

#define PRIiMAX "I64i"
#define PRIiPTR "i"

#define PRIo8 "o"
#define PRIo16 "o"
#define PRIo32 "o"
#define PRIo64 "I64o"

#define PRIoLEAST8 "o"
#define PRIoLEAST16 "o"
#define PRIoLEAST32 "o"
#define PRIoLEAST64 "I64o"

#define PRIoFAST8 "o"
#define PRIoFAST16 "o"
#define PRIoFAST32 "o"
#define PRIoFAST64 "I64o"

#define PRIoMAX "I64o"

#define PRIoPTR "o"

/* fprintf macros for unsigned types */
#define PRIu8 "u"
#define PRIu16 "u"
#define PRIu32 "u"
#define PRIu64 "I64u"


#define PRIuLEAST8 "u"
#define PRIuLEAST16 "u"
#define PRIuLEAST32 "u"
#define PRIuLEAST64 "I64u"

#define PRIuFAST8 "u"
#define PRIuFAST16 "u"
#define PRIuFAST32 "u"
#define PRIuFAST64 "I64u"

#define PRIuMAX "I64u"
#define PRIuPTR "u"

#define PRIx8 "x"
#define PRIx16 "x"
#define PRIx32 "x"
#define PRIx64 "I64x"

#define PRIxLEAST8 "x"
#define PRIxLEAST16 "x"
#define PRIxLEAST32 "x"
#define PRIxLEAST64 "I64x"

#define PRIxFAST8 "x"
#define PRIxFAST16 "x"
#define PRIxFAST32 "x"
#define PRIxFAST64 "I64x"

#define PRIxMAX "I64x"
#define PRIxPTR "x"

#define PRIX8 "X"
#define PRIX16 "X"
#define PRIX32 "X"
#define PRIX64 "I64X"

#define PRIXLEAST8 "X"
#define PRIXLEAST16 "X"
#define PRIXLEAST32 "X"
#define PRIXLEAST64 "I64X"

#define PRIXFAST8 "X"
#define PRIXFAST16 "X"
#define PRIXFAST32 "X"
#define PRIXFAST64 "I64X"

#define PRIXMAX "I64X"
#define PRIXPTR "X"

/*
 *   fscanf macros for signed int types
 *   NOTE: if 32-bit int is used for int_fast8_t and int_fast16_t
 *   (see stdint.h, 7.18.1.3), FAST8 and FAST16 should have
 *   no length identifiers
 */

#define SCNd16 "hd"
#define SCNd32 "d"
#define SCNd64 "I64d"

#define SCNdLEAST16 "hd"
#define SCNdLEAST32 "d"
#define SCNdLEAST64 "I64d"

#define SCNdFAST16 "hd"
#define SCNdFAST32 "d"
#define SCNdFAST64 "I64d"

#define SCNdMAX "I64d"
#define SCNdPTR "d"

#define SCNi16 "hi"
#define SCNi32 "i"
#define SCNi64 "I64i"

#define SCNiLEAST16 "hi"
#define SCNiLEAST32 "i"
#define SCNiLEAST64 "I64i"

#define SCNiFAST16 "hi"
#define SCNiFAST32 "i"
#define SCNiFAST64 "I64i"

#define SCNiMAX "I64i"
#define SCNiPTR "i"

#define SCNo16 "ho"
#define SCNo32 "o"
#define SCNo64 "I64o"

#define SCNoLEAST16 "ho"
#define SCNoLEAST32 "o"
#define SCNoLEAST64 "I64o"

#define SCNoFAST16 "ho"
#define SCNoFAST32 "o"
#define SCNoFAST64 "I64o"

#define SCNoMAX "I64o"
#define SCNoPTR "o"

#define SCNx16 "hx"
#define SCNx32 "x"
#define SCNx64 "I64x"

#define SCNxLEAST16 "hx"
#define SCNxLEAST32 "x"
#define SCNxLEAST64 "I64x"

#define SCNxFAST16 "hx"
#define SCNxFAST32 "x"
#define SCNxFAST64 "I64x"

#define SCNxMAX "I64x"
#define SCNxPTR "x"


/* fscanf macros for unsigned int types */
//感觉这里是无符号
#define SCNu16 "hu"
#define SCNu32 "u"
#define SCNu64 "I64u"

#define SCNuLEAST16 "hu"
#define SCNuLEAST32 "u"
#define SCNuLEAST64 "I64u"

#define SCNuFAST16 "hu"
#define SCNuFAST32 "u"
#define SCNuFAST64 "I64u"

#define SCNuMAX "I64u"
#define SCNuPTR "u"

  我感觉gcc这个源码没有Ubuntu哪个那么容易看懂,管他呢……用法是一样的

百度上的例子:

  PRIi8、PRIu8、PRIo8以及PRIx8,其中i为有符号,u为无符号,o为8进制以及x为16进制。

百度上的代码:

#include <stdio.h>
#include <inttypes.h>
 
int main() {
    int a = 5;
    printf("a=%"PRIu64"\n", a);
    return 0;
}

  输出是啥我不太明白,感觉像是按位把int 拓展成了long long 导致出现了一个特别奇怪的数

我做了其他的测试

    int a = 15;
    printf("a=%"PRx8"\n", a);

输出的是 f 

    int a = 0xff;
    printf("a=%"PRu8"\n", a);

输出的是255

  嘿嘿,真是个进制转换的神器,不过好像没有二进制,而且只有常用的8和16,其他的还得自己写,这个库里还有其他功能,我先研究研究,日后再更新。(现在主要是防止自己忘记)

学习不易,诸君共勉!

原文地址:https://www.cnblogs.com/daker-code/p/12171386.html