蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 C11 预处理指令与条件编译、参数化宏括号保护、# 与 ## 运算符及预定义宏

C本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。explore-preprocessors.c
explore-preprocessors.c79 行
#include <stdio.h>
#include <string.h>

#define MAX_ARRAY_LENGTH 20
#define FILE_SIZE 16
#undef FILE_SIZE
#define FILE_SIZE 42

#if !defined(MESSAGE)
#define MESSAGE "You wish!"
#endif

#define SQUARE(x) ((x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define FORMAT_PAIR(buf, sz, a, b) \
    snprintf((buf), (sz), #a " and " #b ": ready")

#define GET_TOKEN(n) token##n
#define TOKEN_NAME(n) "token" #n

#define FEATURE_LEVEL 3
#if FEATURE_LEVEL > 2
#define ACTIVE_BRANCH "v3"
#elif FEATURE_LEVEL == 2
#define ACTIVE_BRANCH "v2"
#else
#define ACTIVE_BRANCH "v1"
#endif

int main(void) {
    /* Case 1: #define, #undef, #if !defined, and conditional compilation */
    int std_is_c11 = 0;
#if __STDC_VERSION__ >= 201112L
    std_is_c11 = 1;
#endif
    int c1_pass = (MAX_ARRAY_LENGTH == 20 && FILE_SIZE == 42 &&
                   strcmp(MESSAGE, "You wish!") == 0 &&
                   strcmp(ACTIVE_BRANCH, "v3") == 0 && std_is_c11 == 1);

    /* Case 2: parameterized macros with proper parenthesization */
    int sq5 = SQUARE(5);
    int sq_expr = SQUARE(2 + 3); /* ((2 + 3) * (2 + 3)) == 25 */
    int mx = MAX(10, 20);
    int c2_pass = (sq5 == 25 && sq_expr == 25 && mx == 20);

    /* Case 3: line continuation (\), stringification (#), token pasting (##) */
    char pair_buf[64];
    FORMAT_PAIR(pair_buf, sizeof(pair_buf), Carole, Debra);
    int token34 = 40;
    const char *tname = TOKEN_NAME(34);
    int tval = GET_TOKEN(34);
    int c3_pass = (strcmp(pair_buf, "Carole and Debra: ready") == 0 &&
                   strcmp(tname, "token34") == 0 && tval == 40);

    /* Case 4: ANSI C predefined macros (__FILE__, __LINE__, __DATE__, __TIME__, __STDC__) */
    int line_now = __LINE__;
    int stdc_flag = __STDC__;
    int file_ok = (strstr(__FILE__, "explore-preprocessors.c") != NULL);
    int date_ok = (strlen(__DATE__) == 11);
    int time_ok = (strlen(__TIME__) == 8 && __TIME__[2] == ':' && __TIME__[5] == ':');
    int c4_pass = (file_ok && line_now > 0 && date_ok && time_ok && stdc_flag == 1);

    printf("{\n");
    printf("  \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
    printf("  \"cases\": [\n");
    printf("    {\"name\": \"directives_and_conditionals\", \"expected\": \"20|42|v3|c11=1\", \"actual\": \"%d|%d|%s|c11=%d\", \"passed\": %s},\n",
           MAX_ARRAY_LENGTH, FILE_SIZE, ACTIVE_BRANCH, std_is_c11, c1_pass ? "true" : "false");
    printf("    {\"name\": \"parameterized_macros\", \"expected\": [25, 25, 20], \"actual\": [%d, %d, %d], \"passed\": %s},\n",
           sq5, sq_expr, mx, c2_pass ? "true" : "false");
    printf("    {\"name\": \"stringize_and_token_paste\", \"expected\": \"Carole and Debra: ready|token34=40\", \"actual\": \"%s|%s=%d\", \"passed\": %s},\n",
           pair_buf, tname, tval, c3_pass ? "true" : "false");
    printf("    {\"name\": \"predefined_macros\", \"expected\": {\"file_ok\": true, \"date_ok\": true, \"time_ok\": true, \"stdc\": 1}, \"actual\": {\"file_ok\": %s, \"date_ok\": %s, \"time_ok\": %s, \"stdc\": %d}, \"passed\": %s}\n",
           file_ok ? "true" : "false", date_ok ? "true" : "false", time_ok ? "true" : "false", stdc_flag, c4_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-preprocessors-lab/explore-preprocessors.c

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-preprocessors.c,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C

课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip

下载课程关联工程包:C

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3231.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习