#include <stdio.h>
int main(void) {
int a = 21, b = 10;
int add = a + b, sub = a - b, mul = a * b, divv = a / b, modv = a % b;
int x = 10;
int post_c = x++;
int post_x = x;
x = 10;
int pre_c = ++x;
int pre_x = x;
int c1_pass = (add == 31 && sub == 11 && mul == 210 && divv == 2 && modv == 1 &&
post_c == 10 && post_x == 11 && pre_c == 11 && pre_x == 11);
int rel_eq = (21 == 10), rel_ne = (21 != 10), rel_gt = (21 > 10), rel_le = (5 <= 20);
int side = 0;
int sc_and = (0 && ++side);
int sc_or = (1 || ++side);
int not_zero = !0;
int c2_pass = (rel_eq == 0 && rel_ne == 1 && rel_gt == 1 && rel_le == 1 &&
sc_and == 0 && sc_or == 1 && side == 0 && not_zero == 1);
unsigned int ua = 60u, ub = 13u;
int bw_and = (int)(ua & ub);
int bw_or = (int)(ua | ub);
int bw_xor = (int)(ua ^ ub);
int bw_not = (int)(~ua);
int bw_shl = (int)(ua << 2);
int bw_shr = (int)(ua >> 2);
int cv = 21;
cv += 21; cv -= 21; cv *= 21; cv /= 21;
cv = 200; cv %= 21;
cv <<= 2; cv >>= 2; cv &= 2; cv ^= 2; cv |= 2;
int c3_pass = (bw_and == 12 && bw_or == 61 && bw_xor == 49 && bw_not == -61 &&
bw_shl == 240 && bw_shr == 15 && cv == 2);
int m_a = 4;
short m_b = 0;
double m_c = 0.0;
int *ptr = &m_a;
int deref = *ptr;
int tern = (10 == 10) ? 20 : 30;
int p1 = 7 + 3 * 2;
int p2 = (20 + 10) * 15 / 5;
int p3 = 20 + (10 * 15) / 5;
int c4_pass = (sizeof(m_a) == 4 && sizeof(m_b) == 2 && sizeof(m_c) == 8 &&
deref == 4 && tern == 20 && p1 == 13 && p2 == 90 && p3 == 50);
printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
printf(" {\"name\": \"arithmetic_and_inc_dec\", \"expected\": {\"add\": 31, \"div\": 2, \"mod\": 1, \"post_c\": 10, \"pre_c\": 11}, \"actual\": {\"add\": %d, \"div\": %d, \"mod\": %d, \"post_c\": %d, \"pre_c\": %d}, \"passed\": %s},\n",
add, divv, modv, post_c, pre_c, c1_pass ? "true" : "false");
printf(" {\"name\": \"relational_and_logical_short_circuit\", \"expected\": {\"ne\": 1, \"sc_and\": 0, \"sc_or\": 1, \"side\": 0, \"not0\": 1}, \"actual\": {\"ne\": %d, \"sc_and\": %d, \"sc_or\": %d, \"side\": %d, \"not0\": %d}, \"passed\": %s},\n",
rel_ne, sc_and, sc_or, side, not_zero, c2_pass ? "true" : "false");
printf(" {\"name\": \"bitwise_and_compound_assign\", \"expected\": {\"and\": 12, \"or\": 61, \"xor\": 49, \"not\": -61, \"shl\": 240, \"shr\": 15, \"chain\": 2}, \"actual\": {\"and\": %d, \"or\": %d, \"xor\": %d, \"not\": %d, \"shl\": %d, \"shr\": %d, \"chain\": %d}, \"passed\": %s},\n",
bw_and, bw_or, bw_xor, bw_not, bw_shl, bw_shr, cv, c3_pass ? "true" : "false");
printf(" {\"name\": \"misc_and_precedence\", \"expected\": {\"deref\": 4, \"tern\": 20, \"p1\": 13, \"p2\": 90, \"p3\": 50}, \"actual\": {\"deref\": %d, \"tern\": %d, \"p1\": %d, \"p2\": %d, \"p3\": %d}, \"passed\": %s}\n",
deref, tern, p1, p2, p3, 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-operators-lab/explore-operators.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-operators.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3229.zip
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip