• C++标准模板(STL)- 类型支持 (定宽整数类型)(INT8_C,INTMAX_C,UINT8_C,UINTMAX_C,格式化宏常量)


    最小宽度整数常量的函数宏

    INT8_CINT16_CINT32_CINT64_C

    展开成拥有其实参所指定的值且类型分别为 int_least8_t、int_least16_t、int_least32_t、int_least64_t 的整数常量表达式
    (宏函数)

    INTMAX_C

    展开成拥有其实参所指定的值且类型为 intmax_t 的整数常量表达式
    (宏函数)

    UINT8_CUINT16_CUINT32_CUINT64_C

    展开成拥有其实参所指定的值且类型分别为 uint_least8_t、uint_least16_t、uint_least32_t、uint_least64_t 的整数常量表达式
    (宏函数)

    UINTMAX_C

    展开成拥有其实参所指定的值且类型为 uintmax_t 的整数常量表达式
    (宏函数)

    调用示例

    1. #include <iostream>
    2. #include <cstdint>
    3. int main()
    4. {
    5. //展开成拥有其实参所指定的值且类型分别为 int_least8_t、int_least16_t、
    6. //int_least32_t、int_least64_t 的整数常量表达式
    7. std::cout << "INT8_C(8): " << INT8_C(8) << std::endl;
    8. std::cout << "INT16_C(16): " << INT16_C(16) << std::endl;
    9. std::cout << "INT32_C(32): " << INT32_C(32) << std::endl;
    10. std::cout << "INT64_C(64): " << INT64_C(64) << std::endl;
    11. std::cout << std::endl;
    12. //展开成拥有其实参所指定的值且类型分别为 uint_least8_t、uint_least16_t、
    13. //uint_least32_t、uint_least64_t 的整数常量表达式
    14. std::cout << "UINT8_C(8): " << UINT8_C(8) << std::endl;
    15. std::cout << "UINT16_C(16): " << UINT16_C(16) << std::endl;
    16. std::cout << "UINT32_C(32): " << UINT32_C(32) << std::endl;
    17. std::cout << "UINT64_C(64): " << UINT64_C(64) << std::endl;
    18. std::cout << std::endl;
    19. //展开成拥有其实参所指定的值且类型为 intmax_t 的整数常量表达式
    20. std::cout << "INTMAX_C(32): " << INTMAX_C(32) << std::endl;
    21. //展开成拥有其实参所指定的值且类型为 uintmax_t 的整数常量表达式
    22. std::cout << "UINTMAX_C(64): " << UINTMAX_C(64) << std::endl;
    23. std::cout << std::endl;
    24. return 0;
    25. }

    输出

    格式化宏常量

    定义于头文件 

    std::fprintf 系列函数的格式化宏常量


    unsigned int
    的等价者
    描述数据类型的宏



     

    std::intx_t




     

    std::int_leastx_t

    std::int_fastx_t

    std::intmax_t

    std::intptr_t

    x = 8 、 16 、 32 或 64
    d有符号十进制整数值的输入PRIdxPRIdLEASTxPRIdFASTxPRIdMAXPRIdPTR
    iPRIixPRIiLEASTxPRIiFASTxPRIiMAXPRIiPTR
    u无符号十进制整数值的输入PRIuxPRIuLEASTxPRIuFASTxPRIuMAXPRIuPTR
    o无符号八进制整数值的输入PRIoxPRIoLEASTxPRIoFASTxPRIoMAXPRIoPTR
    x无符号小写十六进制整数值的输入PRIxxPRIxLEASTxPRIxFASTxPRIxMAXPRIxPTR
    X无符号大写十六进制整数值的输入PRIXxPRIXLEASTxPRIXFASTxPRIXMAXPRIXPTR

    std::fscanf 系列函数的格式化宏常量 


    unsigned int
    的等价者
    描述数据类型的宏



     

    std::intx_t




     

    std::int_leastx_t

    std::int_fastx_t

    std::intmax_t

    std::intptr_t

    x = 8 、 16 、 32 或 64
    d有符号十进制整数值的输入SCNdxSCNdLEASTxSCNdFASTxSCNdMAXSCNdPTR
    i有符号整数值的输入SCNixSCNiLEASTxSCNiFASTxSCNiMAXSCNiPTR
    u无符号十进制整数值的输入SCNuxSCNuLEASTxSCNuFASTxSCNuMAXSCNuPTR
    o无符号八进制整数值的输入SCNoxSCNoLEASTxSCNoFASTxSCNoMAXSCNoPTR
    x无符号十六进制整数值的输入SCNxxSCNxLEASTxSCNxFASTxSCNxMAXSCNxPTR

    调用示例

    1. #include <cstdio>
    2. #include <cinttypes>
    3. int main()
    4. {
    5. std::printf("INT8_MIN: %+" PRId8 "\n", INT8_MIN);
    6. std::printf("INT16_MIN: %+" PRId16 "\n", INT16_MIN);
    7. std::printf("INT32_MIN: %+" PRId32 "\n", INT32_MIN);
    8. std::printf("INT64_MIN: %+" PRId64 "\n", INT64_MIN);
    9. std::printf("\n");
    10. std::printf("INT_LEAST8_MIN: %+" PRIdLEAST8 "\n", INT_LEAST8_MIN);
    11. std::printf("INT_LEAST16_MIN: %+" PRIdLEAST16 "\n", INT_LEAST16_MIN);
    12. std::printf("INT_LEAST32_MIN: %+" PRIdLEAST32 "\n", INT_LEAST32_MIN);
    13. std::printf("INT_LEAST64_MIN: %+" PRIdLEAST64 "\n", INT_LEAST64_MIN);
    14. std::printf("\n");
    15. std::printf("INT_FAST8_MIN: %+" PRIdFAST8 "\n", INT_FAST8_MIN);
    16. std::printf("INT_FAST16_MIN: %+" PRIdFAST16 "\n", INT_FAST16_MIN);
    17. std::printf("INT_FAST32_MIN: %+" PRIdFAST32 "\n", INT_FAST32_MIN);
    18. std::printf("INT_FAST64_MIN: %+" PRIdFAST64 "\n", INT_FAST64_MIN);
    19. std::printf("\n");
    20. std::printf("INT8_MIN: %+" PRIi8 "\n", INT8_MIN);
    21. std::printf("INT16_MIN: %+" PRIi16 "\n", INT16_MIN);
    22. std::printf("INT32_MIN: %+" PRIi32 "\n", INT32_MIN);
    23. std::printf("INT64_MIN: %+" PRIi64 "\n", INT64_MIN);
    24. std::printf("\n");
    25. std::printf("INT_LEAST8_MIN: %+" PRIiLEAST8 "\n", INT_LEAST8_MIN);
    26. std::printf("INT_LEAST16_MIN: %+" PRIiLEAST16 "\n", INT_LEAST16_MIN);
    27. std::printf("INT_LEAST32_MIN: %+" PRIiLEAST32 "\n", INT_LEAST32_MIN);
    28. std::printf("INT_LEAST64_MIN: %+" PRIiLEAST64 "\n", INT_LEAST64_MIN);
    29. std::printf("\n");
    30. std::printf("INT_FAST8_MIN: %+" PRIiFAST8 "\n", INT_FAST8_MIN);
    31. std::printf("INT_FAST16_MIN: %+" PRIiFAST16 "\n", INT_FAST16_MIN);
    32. std::printf("INT_FAST32_MIN: %+" PRIiFAST32 "\n", INT_FAST32_MIN);
    33. std::printf("INT_FAST64_MIN: %+" PRIiFAST64 "\n", INT_FAST64_MIN);
    34. std::printf("\n");
    35. std::printf("INT8_MIN: %" PRIo8 "\n", INT8_MIN);
    36. std::printf("INT16_MIN: %" PRIo16 "\n", INT16_MIN);
    37. std::printf("INT32_MIN: %" PRIo32 "\n", INT32_MIN);
    38. std::printf("INT64_MIN: %" PRIo64 "\n", INT64_MIN);
    39. std::printf("\n");
    40. std::printf("INT_LEAST8_MIN: %" PRIoLEAST8 "\n", INT_LEAST8_MIN);
    41. std::printf("INT_LEAST16_MIN: %" PRIoLEAST16 "\n", INT_LEAST16_MIN);
    42. std::printf("INT_LEAST32_MIN: %" PRIoLEAST32 "\n", INT_LEAST32_MIN);
    43. std::printf("INT_LEAST64_MIN: %" PRIoLEAST64 "\n", INT_LEAST64_MIN);
    44. std::printf("\n");
    45. std::printf("INT_FAST8_MIN: %" PRIoFAST8 "\n", INT_FAST8_MIN);
    46. std::printf("INT_FAST16_MIN: %" PRIoFAST16 "\n", INT_FAST16_MIN);
    47. std::printf("INT_FAST32_MIN: %" PRIoFAST32 "\n", INT_FAST32_MIN);
    48. std::printf("INT_FAST64_MIN: %" PRIoFAST64 "\n", INT_FAST64_MIN);
    49. std::printf("\n");
    50. std::printf("UINT8_MAX: %" PRIu8 "\n", UINT8_MAX);
    51. std::printf("UINT16_MAX: %" PRIu16 "\n", UINT16_MAX);
    52. std::printf("UINT32_MAX: %" PRIu32 "\n", UINT32_MAX);
    53. std::printf("UINT64_MAX: %" PRIu64 "\n", UINT64_MAX);
    54. std::printf("\n");
    55. std::printf("UINT_LEAST8_MAX: %" PRIuLEAST8 "\n", UINT_LEAST8_MAX);
    56. std::printf("UINT_LEAST16_MAX: %" PRIuLEAST16 "\n", UINT_LEAST16_MAX);
    57. std::printf("UINT_LEAST32_MAX: %" PRIuLEAST32 "\n", UINT_LEAST32_MAX);
    58. std::printf("UINT_LEAST64_MAX: %" PRIuLEAST64 "\n", UINT_LEAST64_MAX);
    59. std::printf("\n");
    60. std::printf("UINT_FAST8_MAX: %" PRIuFAST8 "\n", UINT_FAST8_MAX);
    61. std::printf("UINT_FAST16_MAX: %" PRIuFAST16 "\n", UINT_FAST16_MAX);
    62. std::printf("UINT_FAST32_MAX: %" PRIuFAST32 "\n", UINT_FAST32_MAX);
    63. std::printf("UINT_FAST64_MAX: %" PRIuFAST64 "\n", UINT_FAST64_MAX);
    64. std::printf("\n");
    65. std::printf("UINT8_MAX: %" PRIx8 "\n", UINT8_MAX);
    66. std::printf("UINT16_MAX: %" PRIx16 "\n", UINT16_MAX);
    67. std::printf("UINT32_MAX: %" PRIx32 "\n", UINT32_MAX);
    68. std::printf("UINT64_MAX: %" PRIx64 "\n", UINT64_MAX);
    69. std::printf("\n");
    70. std::printf("UINT_LEAST8_MAX: %" PRIxLEAST8 "\n", UINT_LEAST8_MAX);
    71. std::printf("UINT_LEAST16_MAX: %" PRIxLEAST16 "\n", UINT_LEAST16_MAX);
    72. std::printf("UINT_LEAST32_MAX: %" PRIxLEAST32 "\n", UINT_LEAST32_MAX);
    73. std::printf("UINT_LEAST64_MAX: %" PRIxLEAST64 "\n", UINT_LEAST64_MAX);
    74. std::printf("\n");
    75. std::printf("UINT_FAST8_MAX: %" PRIxFAST8 "\n", UINT_FAST8_MAX);
    76. std::printf("UINT_FAST16_MAX: %" PRIxFAST16 "\n", UINT_FAST16_MAX);
    77. std::printf("UINT_FAST32_MAX: %" PRIxFAST32 "\n", UINT_FAST32_MAX);
    78. std::printf("UINT_FAST64_MAX: %" PRIxFAST64 "\n", UINT_FAST64_MAX);
    79. std::printf("\n");
    80. return 0;
    81. }

    输出

    1. INT8_MIN: -128
    2. INT16_MIN: -32768
    3. INT32_MIN: -2147483648
    4. INT64_MIN: -9223372036854775808
    5. INT_LEAST8_MIN: -128
    6. INT_LEAST16_MIN: -32768
    7. INT_LEAST32_MIN: -2147483648
    8. INT_LEAST64_MIN: -9223372036854775808
    9. INT_FAST8_MIN: -128
    10. INT_FAST16_MIN: -32768
    11. INT_FAST32_MIN: -2147483648
    12. INT_FAST64_MIN: -9223372036854775808
    13. INT8_MIN: -128
    14. INT16_MIN: -32768
    15. INT32_MIN: -2147483648
    16. INT64_MIN: -9223372036854775808
    17. INT_LEAST8_MIN: -128
    18. INT_LEAST16_MIN: -32768
    19. INT_LEAST32_MIN: -2147483648
    20. INT_LEAST64_MIN: -9223372036854775808
    21. INT_FAST8_MIN: -128
    22. INT_FAST16_MIN: -32768
    23. INT_FAST32_MIN: -2147483648
    24. INT_FAST64_MIN: -9223372036854775808
    25. INT8_MIN: 37777777600
    26. INT16_MIN: 37777700000
    27. INT32_MIN: 20000000000
    28. INT64_MIN: 1000000000000000000000
    29. INT_LEAST8_MIN: 37777777600
    30. INT_LEAST16_MIN: 37777700000
    31. INT_LEAST32_MIN: 20000000000
    32. INT_LEAST64_MIN: 1000000000000000000000
    33. INT_FAST8_MIN: 37777777600
    34. INT_FAST16_MIN: 37777700000
    35. INT_FAST32_MIN: 20000000000
    36. INT_FAST64_MIN: 1000000000000000000000
    37. UINT8_MAX: 255
    38. UINT16_MAX: 65535
    39. UINT32_MAX: 4294967295
    40. UINT64_MAX: 18446744073709551615
    41. UINT_LEAST8_MAX: 255
    42. UINT_LEAST16_MAX: 65535
    43. UINT_LEAST32_MAX: 4294967295
    44. UINT_LEAST64_MAX: 18446744073709551615
    45. UINT_FAST8_MAX: 255
    46. UINT_FAST16_MAX: 65535
    47. UINT_FAST32_MAX: 4294967295
    48. UINT_FAST64_MAX: 18446744073709551615
    49. UINT8_MAX: ff
    50. UINT16_MAX: ffff
    51. UINT32_MAX: ffffffff
    52. UINT64_MAX: ffffffffffffffff
    53. UINT_LEAST8_MAX: ff
    54. UINT_LEAST16_MAX: ffff
    55. UINT_LEAST32_MAX: ffffffff
    56. UINT_LEAST64_MAX: ffffffffffffffff
    57. UINT_FAST8_MAX: ff
    58. UINT_FAST16_MAX: ffff
    59. UINT_FAST32_MAX: ffffffff
    60. UINT_FAST64_MAX: ffffffffffffffff

    注意

    因为 C++ 转译立即后随字符串字面量的字符为用户定义字面量,诸如 printf("%"PRId64"\n",n); 的 C 代码在 C++ 中非法,并在 PRId64 前要求空格。

    C99 标准建议 C++ 实现不应定义上述极限、常量或格式化宏,除非在包含相对的 C 头文件( stdint.hinttypes.h )前(分别)定义宏 __STDC_LIMIT_MACROS__STDC_CONSTANT_MACROS__STDC_FORMAT_MACROS 。此建议未被采纳,并在 C11 中被移除。然而,某些实现(例如 glibc 2.17 )尝试应用此规则,而可能需要定义 __STDC 宏; C++ 编译器可以通过在某些环境自动定义它们来做变通。

  • 相关阅读:
    索引模型的常见数据结构
    集成学习-Bagging和Boosting算法
    pytest-rerunfailures插件之测试用例失败重跑
    Pyecharts一文速学-绘制桑基图详解+Python代码
    用核心AI资产打造稀缺电竞体验,顺网灵悉背后有一盘大棋
    virtualbox虚拟机安装在笔记本上使用WIFI无法上网
    RabbitMQ 快速入门-消息的收发
    使用 Hibernate Envers 进行实体审计
    【Spring Cloud】新闻头条微服务项目:自媒体文章管理
    vue钩子函数以及例子
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/133432310