#include <stdint.h>
#include <stdio.h>
#include <string.h>
typedef struct {
int id;
int correct_index;
} QuizItem;
static int score_quiz(const QuizItem *items, const int *answers, size_t n) {
int score = 0;
for (size_t i = 0; i < n; i++) {
if (answers[i] == items[i].correct_index) score++;
}
return score;
}
int main(void) {
/* Case 1: Q8 — 16-bit signed integer range (-32768 to 32767) via <stdint.h> */
int16_t min16 = INT16_MIN;
int16_t max16 = INT16_MAX;
int c1_pass = (min16 == -32768 && max16 == 32767 && sizeof(int16_t) == 2);
/* Case 2: Q3 & Q9 — source file suffix (.c) and ISO C11 standard compiler check */
const char *src_file = __FILE__;
size_t flen = strlen(src_file);
int ends_with_c = (flen >= 2 && src_file[flen - 2] == '.' && src_file[flen - 1] == 'c');
long std_ver = __STDC_VERSION__;
int c2_pass = (ends_with_c && __STDC__ == 1 && std_ver >= 201112L);
/* Case 3: 10-question C fundamentals quiz scoring logic (perfect + partial) */
const QuizItem bank[10] = {
{1, 3}, {2, 0}, {3, 3}, {4, 3}, {5, 3},
{6, 1}, {7, 3}, {8, 0}, {9, 0}, {10, 2}
};
const int perfect_ans[10] = {3, 0, 3, 3, 3, 1, 3, 0, 0, 2};
const int partial_ans[10] = {3, 1, 3, 3, 3, 1, 0, 0, 0, 2};
int s_perf = score_quiz(bank, perfect_ans, 10);
int s_part = score_quiz(bank, partial_ans, 10);
int c3_pass = (s_perf == 10 && s_part == 8);
printf("{\n");
printf(" \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
printf(" \"cases\": [\n");
printf(" {\"name\": \"int16_range_check\", \"expected\": [-32768, 32767], \"actual\": [%d, %d], \"passed\": %s},\n",
(int)min16, (int)max16, c1_pass ? "true" : "false");
printf(" {\"name\": \"c_source_suffix_and_compiler\", \"expected\": {\"dot_c\": true, \"c11\": true}, \"actual\": {\"dot_c\": %s, \"c11\": %s}, \"passed\": %s},\n",
ends_with_c ? "true" : "false", (std_ver >= 201112L) ? "true" : "false", c2_pass ? "true" : "false");
printf(" {\"name\": \"quiz_10q_scoring_engine\", \"expected\": [10, 8], \"actual\": [%d, %d], \"passed\": %s}\n",
s_perf, s_part, c3_pass ? "true" : "false");
printf(" ]\n");
printf("}\n");
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。
入口文件:projects/c-quiz-lab/explore-quiz.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-quiz.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3234.zip