#include <stdatomic.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
_Static_assert(sizeof(int) >= 4, "int must be at least 4 bytes");
#define TYPE_TAG(x) _Generic((x), \
int: "int", \
double: "double", \
const char *: "string", \
default: "other")
struct Vec2 {
union {
struct {
int x;
int y;
};
int coords[2];
};
};
int main(void) {
_Alignas(16) unsigned char aligned_buf[32];
size_t align_double = alignof(double);
int is_aligned_16 = (((size_t)aligned_buf % 16) == 0);
int c1_pass = (is_aligned_16 && align_double == 8);
int iv = 42;
double dv = 3.14;
const char *sv = "c11";
const char *t1 = TYPE_TAG(iv);
const char *t2 = TYPE_TAG(dv);
const char *t3 = TYPE_TAG(sv);
int c2_pass = (strcmp(t1, "int") == 0 && strcmp(t2, "double") == 0 && strcmp(t3, "string") == 0);
struct Vec2 v;
v.x = 12;
v.y = 34;
int c3_pass = (v.coords[0] == 12 && v.coords[1] == 34);
atomic_int counter = ATOMIC_VAR_INIT(10);
int prev = atomic_fetch_add(&counter, 5);
int curr = atomic_load(&counter);
const char *tmp_path = "/tmp/c11_intro_wx_test.txt";
remove(tmp_path);
FILE *f1 = fopen(tmp_path, "wx");
int first_created = (f1 != NULL);
if (f1) {
fputs("safe-line\n", f1);
fclose(f1);
}
FILE *f2 = fopen(tmp_path, "wx");
int second_rejected = (f2 == NULL);
if (f2) {
fclose(f2);
}
char line[32] = "";
FILE *fr = fopen(tmp_path, "r");
if (fr) {
if (fgets(line, sizeof(line), fr) == NULL) {
line[0] = '\0';
}
fclose(fr);
}
remove(tmp_path);
int read_ok = (strcmp(line, "safe-line\n") == 0);
int c4_pass = (prev == 10 && curr == 15 && first_created && second_rejected && read_ok);
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"c11_alignment_and_static_assert\", \"expected\": {\"aligned_16\": true, \"alignof_double\": 8}, \"actual\": {\"aligned_16\": %s, \"alignof_double\": %zu}, \"passed\": %s},\n",
is_aligned_16 ? "true" : "false", align_double, c1_pass ? "true" : "false");
printf(" {\"name\": \"c11_generic_selection\", \"expected\": [\"int\", \"double\", \"string\"], \"actual\": [\"%s\", \"%s\", \"%s\"], \"passed\": %s},\n",
t1, t2, t3, c2_pass ? "true" : "false");
printf(" {\"name\": \"c11_anonymous_struct_union\", \"expected\": {\"x\": 12, \"y\": 34}, \"actual\": {\"x\": %d, \"y\": %d}, \"passed\": %s},\n",
v.coords[0], v.coords[1], c3_pass ? "true" : "false");
printf(" {\"name\": \"c11_atomic_and_fopen_wx\", \"expected\": {\"prev\": 10, \"curr\": 15, \"wx_first_ok\": true, \"wx_second_fail\": true, \"fgets_ok\": true}, \"actual\": {\"prev\": %d, \"curr\": %d, \"wx_first_ok\": %s, \"wx_second_fail\": %s, \"fgets_ok\": %s}, \"passed\": %s}\n",
prev, curr,
first_created ? "true" : "false",
second_rejected ? "true" : "false",
read_ok ? "true" : "false",
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-intro-lab/explore-intro.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-intro.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3226.zip