蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 <inttypes.h> 的 PRI* 输出宏、SCN* 解析宏、极值打印与 imaxabs/imaxdiv

C本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。explore-standard-library-inttypes-h.c
explore-standard-library-inttypes-h.c55 行
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdbool.h>

int main(void) {
    int8_t i8 = -8;
    uint8_t u8 = 250;
    int16_t i16 = -1600;
    uint16_t u16 = 60000;
    int32_t i32 = 42;
    uint32_t u32 = 3000000000U;
    int64_t i64 = -123456789012345LL;
    uint64_t u64 = 1234567890123456789ULL;

    char formatted[160];
    snprintf(
        formatted, sizeof(formatted),
        "%" PRId8 ",%" PRIu8 ",%" PRId16 ",%" PRIu16 ",%" PRId32 ",%" PRIu32 ",%" PRId64 ",%" PRIu64,
        i8, u8, i16, u16, i32, u32, i64, u64
    );
    const char *exp1 = "-8,250,-1600,60000,42,3000000000,-123456789012345,1234567890123456789";
    bool c1_pass = (strcmp(formatted, exp1) == 0);

    int32_t p32 = 0;
    uint64_t p64 = 0;
    int matched = sscanf("-42 1234567890123456789", "%" SCNd32 " %" SCNu64, &p32, &p64);
    bool c2_pass = (matched == 2 && p32 == -42 && p64 == 1234567890123456789ULL);

    char lim_buf[96];
    snprintf(lim_buf, sizeof(lim_buf), "%" PRId32 "|%" PRIu64, INT32_MIN, UINT64_MAX);
    intmax_t abs_val = imaxabs(-9876543210LL);
    imaxdiv_t qr = imaxdiv(100, 7);
    bool c3_pass = (
        strcmp(lim_buf, "-2147483648|18446744073709551615") == 0 &&
        abs_val == 9876543210LL &&
        qr.quot == 14 && qr.rem == 2
    );

    printf(
        "{"
        "\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
        "\"cases\":["
        "{\"name\":\"printf_pri_macros_formatting\",\"expected\":\"%s\",\"actual\":\"%s\",\"passed\":%s},"
        "{\"name\":\"scanf_scn_macros_roundtrip\",\"expected\":\"n=2,p32=-42,p64=1234567890123456789\",\"actual\":\"n=%d,p32=%" PRId32 ",p64=%" PRIu64 "\",\"passed\":%s},"
        "{\"name\":\"limits_and_imax_helpers\",\"expected\":\"-2147483648|18446744073709551615,q=14,r=2\",\"actual\":\"%s,q=%" PRIdMAX ",r=%" PRIdMAX "\",\"passed\":%s}"
        "]"
        "}\n",
        exp1, formatted, c1_pass ? "true" : "false",
        matched, p32, p64, c2_pass ? "true" : "false",
        lim_buf, qr.quot, qr.rem, c3_pass ? "true" : "false"
    );
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。

入口文件:projects/c-standard-library-inttypes-h-lab/explore-standard-library-inttypes-h.c

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-standard-library-inttypes-h.c,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3246.zip

下载课程关联工程包:C

课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习