#include <float.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
static int no_param_func(void) {
return 77;
}
static void write_via_void_ptr(void *dst, int val) {
*(int *)dst = val;
}
int main(void) {
size_t sz_char = sizeof(char);
size_t sz_short = sizeof(short);
size_t sz_int = sizeof(int);
size_t sz_long = sizeof(long);
int c1_pass = (sz_char == 1 && sz_short == 2 && sz_int == 4 && sz_long == 8 &&
UCHAR_MAX == 255 && SHRT_MAX == 32767 && INT_MAX == 2147483647 && CHAR_BIT == 8);
size_t sz_flt = sizeof(float);
size_t sz_dbl = sizeof(double);
size_t sz_ldbl = sizeof(long double);
int c2_pass = (sz_flt == 4 && sz_dbl == 8 && sz_ldbl == 16 &&
FLT_DIG == 6 && DBL_DIG == 15 && FLT_MAX > 3.0e38f && FLT_MIN < 1.2e-38f);
int ret_void_param = no_param_func();
void *raw = malloc(sizeof(int));
int roundtrip = 0;
if (raw != NULL) {
write_via_void_ptr(raw, 2026);
roundtrip = *(int *)raw;
free(raw);
}
int c3_pass = (ret_void_param == 77 && roundtrip == 2026);
int iv = 10;
float fv = 3.25f;
double implicit_d = iv + fv;
double pi_d = 3.14159;
int explicit_i = (int)pi_d;
int int_div = 7 / 2;
double cast_div = (double)7 / 2;
int c4_pass = (implicit_d == 13.25 && explicit_i == 3 && int_div == 3 && cast_div == 3.5);
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"integer_sizes_and_limits\", \"expected\": {\"char\": 1, \"short\": 2, \"int\": 4, \"long\": 8, \"uchar_max\": 255, \"int_max\": 2147483647}, \"actual\": {\"char\": %zu, \"short\": %zu, \"int\": %zu, \"long\": %zu, \"uchar_max\": %d, \"int_max\": %d}, \"passed\": %s},\n",
sz_char, sz_short, sz_int, sz_long, UCHAR_MAX, INT_MAX, c1_pass ? "true" : "false");
printf(" {\"name\": \"floating_point_properties\", \"expected\": {\"float\": 4, \"double\": 8, \"long_double\": 16, \"flt_dig\": 6, \"dbl_dig\": 15}, \"actual\": {\"float\": %zu, \"double\": %zu, \"long_double\": %zu, \"flt_dig\": %d, \"dbl_dig\": %d}, \"passed\": %s},\n",
sz_flt, sz_dbl, sz_ldbl, FLT_DIG, DBL_DIG, c2_pass ? "true" : "false");
printf(" {\"name\": \"void_function_and_pointer\", \"expected\": {\"no_param\": 77, \"void_ptr_val\": 2026}, \"actual\": {\"no_param\": %d, \"void_ptr_val\": %d}, \"passed\": %s},\n",
ret_void_param, roundtrip, c3_pass ? "true" : "false");
printf(" {\"name\": \"implicit_and_explicit_casts\", \"expected\": {\"implicit\": 13.25, \"trunc\": 3, \"int_div\": 3, \"cast_div\": 3.5}, \"actual\": {\"implicit\": %.2f, \"trunc\": %d, \"int_div\": %d, \"cast_div\": %.1f}, \"passed\": %s}\n",
implicit_d, explicit_i, int_div, cast_div, c4_pass ? "true" : "false");
printf("]}\n");
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。
入口文件:projects/c-data-types-lab/explore-data-types.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-data-types.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3215.zip