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

实测 <stdint.h> 固定宽度、最小/最快宽度、最大宽度与 uintptr_t 指针往返

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

int main(void) {
    int sizes_ok = (
        sizeof(int8_t) == 1 && sizeof(uint8_t) == 1 &&
        sizeof(int16_t) == 2 && sizeof(uint16_t) == 2 &&
        sizeof(int32_t) == 4 && sizeof(uint32_t) == 4 &&
        sizeof(int64_t) == 8 && sizeof(uint64_t) == 8
    ) ? 1 : 0;
    int limits_ok = (
        INT8_MIN == -128 && INT8_MAX == 127 && UINT8_MAX == 255 &&
        INT16_MIN == -32768 && INT16_MAX == 32767 && UINT16_MAX == 65535 &&
        INT32_MIN == (-2147483647 - 1) && INT32_MAX == 2147483647 && UINT32_MAX == 4294967295U &&
        INT64_MAX == 9223372036854775807LL && UINT64_MAX == 18446744073709551615ULL
    ) ? 1 : 0;
    bool c1_pass = (sizes_ok == 1 && limits_ok == 1);

    int least_ok = (
        sizeof(int_least8_t) >= 1 && sizeof(uint_least8_t) >= 1 &&
        sizeof(int_least16_t) >= 2 && sizeof(uint_least16_t) >= 2 &&
        sizeof(int_least32_t) >= 4 && sizeof(uint_least32_t) >= 4 &&
        sizeof(int_least64_t) >= 8 && sizeof(uint_least64_t) >= 8
    ) ? 1 : 0;
    int fast_ok = (
        sizeof(int_fast8_t) >= 1 && sizeof(uint_fast8_t) >= 1 &&
        sizeof(int_fast16_t) >= 2 && sizeof(uint_fast16_t) >= 2 &&
        sizeof(int_fast32_t) >= 4 && sizeof(uint_fast32_t) >= 4 &&
        sizeof(int_fast64_t) >= 8 && sizeof(uint_fast64_t) >= 8
    ) ? 1 : 0;
    bool c2_pass = (least_ok == 1 && fast_ok == 1);

    int target = 777;
    uintptr_t raw_addr = (uintptr_t)&target;
    intptr_t signed_addr = (intptr_t)&target;
    int restored = *(int *)raw_addr;
    int ptr_ok = (restored == 777 && (void *)signed_addr == (void *)&target) ? 1 : 0;
    int max_ok = (sizeof(intmax_t) >= sizeof(int64_t) && INTMAX_MAX >= INT64_MAX && UINTMAX_MAX >= UINT64_MAX) ? 1 : 0;
    bool c3_pass = (ptr_ok == 1 && max_ok == 1);

    printf(
        "{"
        "\"runtime\":\"GCC C11 (mstorsjo/llvm-mingw:20260616)\","
        "\"cases\":["
        "{\"name\":\"fixed_width_sizes_and_limits\",\"expected\":\"sizes=1,limits=1\",\"actual\":\"sizes=%d,limits=%d\",\"passed\":%s},"
        "{\"name\":\"least_and_fast_width_guarantees\",\"expected\":\"least=1,fast=1\",\"actual\":\"least=%d,fast=%d\",\"passed\":%s},"
        "{\"name\":\"intmax_and_uintptr_roundtrip\",\"expected\":\"ptr=1,max=1,val=777\",\"actual\":\"ptr=%d,max=%d,val=%d\",\"passed\":%s}"
        "]"
        "}\n",
        sizes_ok, limits_ok, c1_pass ? "true" : "false",
        least_ok, fast_ok, c2_pass ? "true" : "false",
        ptr_ok, max_ok, restored, 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-stdint-h-lab/explore-standard-library-stdint-h.c

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

下载课程关联工程包:C

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

下载课程关联工程包:C

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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