#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GREETING_MSG "Hello, World!"
static int combine_units(int a, int b);
int main(void) {
/* 1. Five building blocks: preprocessor, function, variable, statement, comment */
const char *msg = GREETING_MSG;
char out[64];
int n = snprintf(out, sizeof(out), "%s \n", msg);
int c1_pass = (n == 15 && strcmp(out, "Hello, World! \n") == 0);
/* 2. Comment stripping & standard exit code */
int expr_val = 10 /* inline comment */ + 20;
int exit_ok = EXIT_SUCCESS;
int c2_pass = (expr_val == 30 && exit_ok == 0);
/* 3. Forward declaration resolved by definition below (multi-unit pattern) */
int combined = combine_units(17, 25);
int c3_pass = (combined == 42);
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"hello_world_components\", \"expected\": {\"len\": 15, \"matched\": true}, \"actual\": {\"len\": %d, \"matched\": %s}, \"passed\": %s},\n",
n, c1_pass ? "true" : "false", c1_pass ? "true" : "false");
printf(" {\"name\": \"preprocessor_and_main_return\", \"expected\": {\"expr_val\": 30, \"exit_success\": 0}, \"actual\": {\"expr_val\": %d, \"exit_success\": %d}, \"passed\": %s},\n",
expr_val, exit_ok, c2_pass ? "true" : "false");
printf(" {\"name\": \"multi_unit_linkage_simulation\", \"expected\": 42, \"actual\": %d, \"passed\": %s}\n",
combined, c3_pass ? "true" : "false");
printf("]}\n");
return 0;
}
static int combine_units(int a, int b) {
return a + b;
}
本例需在 本地离线 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-program-structure-lab/explore-program-structure.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-program-structure.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3232.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip