#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <locale.h>
#include <setjmp.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Conditional / computed header inclusion via macro */
#define SELECTED_MATH_H <math.h>
#include SELECTED_MATH_H
/* Simulate include-guard idempotency across repeated inclusion */
#ifndef DEMO_HEADER_H
#define DEMO_HEADER_H
#define GUARD_INIT_COUNT 1
typedef struct {
int code;
} HeaderSample;
static const char *header_greeting(void) { return "header-ok"; }
#endif
#ifndef DEMO_HEADER_H
#define DEMO_HEADER_H
#error "Include guard failed to skip duplicate block"
#endif
static jmp_buf jump_env;
int main(void) {
/* Case 1: include guard prevents redefinition and exposes prototype */
HeaderSample hs = {GUARD_INIT_COUNT};
const char *msg = header_greeting();
int c1_pass = (hs.code == 1 && strcmp(msg, "header-ok") == 0);
/* Case 2: macro-computed conditional #include <math.h> works */
double root = sqrt(81.0);
double c0 = cos(0.0);
int c2_pass = (root == 9.0 && c0 == 1.0);
/* Case 3: core C standard headers (<string.h>, <ctype.h>, <limits.h>, <float.h>, <stddef.h>, <time.h>) */
size_t len = strlen("C11");
int is_a = isalpha((unsigned char)'C') != 0;
int is_d = isdigit((unsigned char)'1') != 0;
int limits_ok = (INT_MAX >= 32767 && FLT_MAX > 1e30f);
size_t off = offsetof(HeaderSample, code);
time_t now = time(NULL);
int c3_pass = (len == 3 && is_a && is_d && limits_ok && off == 0 && now != (time_t)-1);
/* Case 4: diagnostic & runtime control headers (<assert.h>, <errno.h>, <signal.h>, <setjmp.h>, <locale.h>) */
assert(len == 3);
errno = 0;
const char *loc = setlocale(LC_ALL, "C");
int jumped = 0;
if (setjmp(jump_env) == 0) {
longjmp(jump_env, 7);
} else {
jumped = 1;
}
int sig_ok = (SIGINT != 0);
int c4_pass = (errno == 0 && loc != NULL && jumped == 1 && sig_ok);
printf("{\n");
printf(" \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
printf(" \"cases\": [\n");
printf(" {\"name\": \"include_guard_idempotency\", \"expected\": \"1|header-ok\", \"actual\": \"%d|%s\", \"passed\": %s},\n",
hs.code, msg, c1_pass ? "true" : "false");
printf(" {\"name\": \"computed_header_include\", \"expected\": [9.0, 1.0], \"actual\": [%.1f, %.1f], \"passed\": %s},\n",
root, c0, c2_pass ? "true" : "false");
printf(" {\"name\": \"core_stdlib_headers\", \"expected\": {\"len\": 3, \"alpha\": true, \"digit\": true, \"limits\": true, \"offset\": 0}, \"actual\": {\"len\": %zu, \"alpha\": %s, \"digit\": %s, \"limits\": %s, \"offset\": %zu}, \"passed\": %s},\n",
len, is_a ? "true" : "false", is_d ? "true" : "false", limits_ok ? "true" : "false", off, c3_pass ? "true" : "false");
printf(" {\"name\": \"diagnostic_runtime_headers\", \"expected\": {\"errno\": 0, \"jumped\": 1, \"sig\": true}, \"actual\": {\"errno\": %d, \"jumped\": %d, \"sig\": %s}, \"passed\": %s}\n",
errno, jumped, sig_ok ? "true" : "false", c4_pass ? "true" : "false");
printf(" ]\n");
printf("}\n");
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-header-files-lab/explore-header-files.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-header-files.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3224.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip