#include <stdalign.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PI_APPROX 3.14159
#define MAX_PAIR(a, b) ((a) > (b) ? (a) : (b))
_Static_assert(sizeof(char) == 1, "char must be 1 byte");
static int add_ints(int a, int b);
static inline int double_restrict(const int *restrict src, int *restrict dst) {
*dst = (*src) * 2;
return *dst;
}
int main(void) {
int a, b, c;
a = b = c = 0;
int sum = add_ints(12, 30);
int maxv = MAX_PAIR(18, 9);
int c1_pass = (a == 0 && b == 0 && c == 0 && sum == 42 && maxv == 18 && EXIT_SUCCESS == 0);
int Manpower = 10;
int manpower = 25;
const char *concat = "Hello, " "World!";
size_t sz_hello = sizeof("Hello");
size_t sz_str_a = sizeof("A");
size_t sz_chr_a = sizeof('A');
int esc_ok = ('\n' == 10 && '\t' == 9 && '\\' == 92 && '\'' == 39 && '\0' == 0);
int c2_pass = (Manpower != manpower && strcmp(concat, "Hello, World!") == 0 &&
sz_hello == 6 && sz_str_a == 2 && sz_chr_a == sizeof(int) && esc_ok);
int x = 5, y = 10;
int add_xy = x + y;
int int_div = 7 / 2;
int int_mod = 7 % 2;
int eq = (x == y);
int lshift = x << 1;
int post_before = x++;
int ternary_max = (x > y) ? x : y;
int wait_cnt = 3;
while (--wait_cnt > 0)
;
int c3_pass = (add_xy == 15 && int_div == 3 && int_mod == 1 && eq == 0 &&
lshift == 10 && post_before == 5 && x == 6 && ternary_max == 10 && wait_cnt == 0);
_Bool flag = 5;
int in_val = 21, out_val = 0;
int doubled = double_restrict(&in_val, &out_val);
size_t align_int = _Alignof(int);
int gen_res = _Generic(in_val, int: 100, default: 0);
int c4_pass = (flag == 1 && doubled == 42 && out_val == 42 && align_int == 4 && gen_res == 100);
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"tokens_structure_and_add\", \"expected\": {\"chain_zero\": true, \"sum\": 42, \"max\": 18}, \"actual\": {\"chain_zero\": %s, \"sum\": %d, \"max\": %d}, \"passed\": %s},\n",
(a == 0 && b == 0 && c == 0) ? "true" : "false", sum, maxv, c1_pass ? "true" : "false");
printf(" {\"name\": \"identifiers_literals_and_escapes\", \"expected\": {\"case_distinct\": true, \"sizeof_hello\": 6, \"sizeof_str_A\": 2, \"sizeof_char_literal\": 4}, \"actual\": {\"case_distinct\": %s, \"sizeof_hello\": %zu, \"sizeof_str_A\": %zu, \"sizeof_char_literal\": %zu}, \"passed\": %s},\n",
(Manpower != manpower) ? "true" : "false", sz_hello, sz_str_a, sz_chr_a, c2_pass ? "true" : "false");
printf(" {\"name\": \"operators_and_separators\", \"expected\": {\"sum\": 15, \"div\": 3, \"mod\": 1, \"lshift\": 10, \"post\": 5, \"after\": 6, \"max\": 10}, \"actual\": {\"sum\": %d, \"div\": %d, \"mod\": %d, \"lshift\": %d, \"post\": %d, \"after\": %d, \"max\": %d}, \"passed\": %s},\n",
add_xy, int_div, int_mod, lshift, post_before, x, ternary_max, c3_pass ? "true" : "false");
printf(" {\"name\": \"c99_and_c11_keywords\", \"expected\": {\"bool_norm\": 1, \"doubled\": 42, \"alignof_int\": 4, \"generic\": 100}, \"actual\": {\"bool_norm\": %d, \"doubled\": %d, \"alignof_int\": %zu, \"generic\": %d}, \"passed\": %s}\n",
(int)flag, doubled, align_int, gen_res, c4_pass ? "true" : "false");
printf("]}\n");
return 0;
}
static int add_ints(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-basic-syntax-lab/explore-basic-syntax.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-basic-syntax.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3211.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip