#include <stdio.h>
#include <string.h>
/* Function prototype omitting parameter names */
static int max_int(int, int);
static void mutate_by_value(int v) {
v = 999;
(void)v;
}
static void swap_by_ptr(int *x, int *y) {
int tmp = *x;
*x = *y;
*y = tmp;
}
static void add_into(int *acc, int delta) {
*acc += delta;
}
int main(void) {
int m1 = max_int(100, 200);
int m2 = max_int(-5, -12);
int c1_pass = (m1 == 200 && m2 == -5);
int orig = 10;
mutate_by_value(orig);
int c2_pass = (orig == 10);
int a = 100, b = 200;
swap_by_ptr(&a, &b);
int c3_pass = (a == 200 && b == 100);
int acc = 5;
add_into(&acc, 15);
char dst[32] = "hello-";
strcat(dst, "c11");
int src_arr[3] = {4, 5, 6};
int copy_arr[3] = {0, 0, 0};
memcpy(copy_arr, src_arr, sizeof(src_arr));
int c4_pass = (acc == 20 && strcmp(dst, "hello-c11") == 0 &&
copy_arr[0] == 4 && copy_arr[1] == 5 && copy_arr[2] == 6);
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"prototype_and_max_call\", \"expected\": [200, -5], \"actual\": [%d, %d], \"passed\": %s},\n",
m1, m2, c1_pass ? "true" : "false");
printf(" {\"name\": \"pass_by_value_isolation\", \"expected\": 10, \"actual\": %d, \"passed\": %s},\n",
orig, c2_pass ? "true" : "false");
printf(" {\"name\": \"pass_by_pointer_mutation\", \"expected\": {\"a\": 200, \"b\": 100}, \"actual\": {\"a\": %d, \"b\": %d}, \"passed\": %s},\n",
a, b, c3_pass ? "true" : "false");
printf(" {\"name\": \"void_function_and_stdlib_calls\", \"expected\": {\"acc\": 20, \"cat\": \"hello-c11\", \"copied_sum\": 15}, \"actual\": {\"acc\": %d, \"cat\": \"%s\", \"copied_sum\": %d}, \"passed\": %s}\n",
acc, dst, copy_arr[0] + copy_arr[1] + copy_arr[2], c4_pass ? "true" : "false");
printf("]}\n");
return 0;
}
static int max_int(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
本例需在 本地离线 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-functions-lab/explore-functions.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-functions.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3223.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip