乐闻世界logo
搜索文章和话题

C语言中预定义宏和条件编译的完整用法是什么?

2月18日 17:21

C语言中预定义宏和条件编译的完整用法是什么?

预定义宏:

  1. 标准预定义宏

    c
    void print_predefined_macros() { printf("File: %s\n", __FILE__); printf("Line: %d\n", __LINE__); printf("Date: %s\n", __DATE__); printf("Time: %s\n", __TIME__); printf("Function: %s\n", __func__); #ifdef __STDC_VERSION__ printf("C Standard: %ld\n", __STDC_VERSION__); #endif }
  2. 编译器特定宏

    c
    void compiler_specific_code() { #ifdef __GNUC__ printf("GCC compiler\n"); #elif defined(__clang__) printf("Clang compiler\n"); #elif defined(_MSC_VER) printf("MSVC compiler\n"); #endif }
  3. 平台检测

    c
    void platform_specific_code() { #ifdef _WIN32 printf("Windows platform\n"); #elif defined(__linux__) printf("Linux platform\n"); #elif defined(__APPLE__) printf("macOS platform\n"); #endif }

条件编译:

  1. 基本条件编译

    c
    #ifdef DEBUG #define LOG(x) printf x #else #define LOG(x) #endif void function() { LOG(("Debug message\n")); }
  2. 多重条件

    c
    #if defined(UNIX) && !defined(DEBUG) #define OPTIMIZED_CODE #endif #if VERSION >= 2 #include "new_features.h" #else #include "legacy_features.h" #endif
  3. 错误和警告

    c
    #if SIZE < 0 #error "Size must be positive" #endif #if !defined(VERSION) #warning "VERSION not defined, using default" #define VERSION "1.0" #endif

高级用法:

  1. 宏字符串化

    c
    #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) printf("Line: %s\n", TOSTRING(__LINE__));
  2. 宏连接

    c
    #define CONCAT(a, b) a##b int CONCAT(var, 1) = 10; // var1
  3. 可变参数宏

    c
    #define LOG(fmt, ...) printf("[LOG] " fmt "\n", ##__VA_ARGS__) LOG("Value: %d", 42); LOG("Simple message");

实际应用:

  1. 调试宏

    c
    #ifdef DEBUG #define DEBUG_PRINT(fmt, ...) \ printf("[DEBUG %s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) #else #define DEBUG_PRINT(fmt, ...) #endif void example_function() { DEBUG_PRINT("Processing data: %d", 100); }
  2. 版本控制

    c
    #define VERSION_MAJOR 2 #define VERSION_MINOR 5 #define VERSION_PATCH 1 #define VERSION_STRING \ TOSTRING(VERSION_MAJOR) "." \ TOSTRING(VERSION_MINOR) "." \ TOSTRING(VERSION_PATCH) printf("Version: %s\n", VERSION_STRING);
  3. 跨平台代码

    c
    #ifdef _WIN32 #define PATH_SEPARATOR '\\' #define PATH_SEPARATOR_STR "\\" #else #define PATH_SEPARATOR '/' #define PATH_SEPARATOR_STR "/" #endif void join_path(char *dest, const char *dir, const char *file) { sprintf(dest, "%s%s%s", dir, PATH_SEPARATOR_STR, file); }
  4. 功能开关

    c
    #define FEATURE_A_ENABLED 1 #define FEATURE_B_ENABLED 0 void process_data() { #if FEATURE_A_ENABLED feature_a_process(); #endif #if FEATURE_B_ENABLED feature_b_process(); #endif }

最佳实践:

  1. 头文件保护

    c
    #ifndef HEADER_H #define HEADER_H // 头文件内容 #endif
  2. 一次性包含

    c
    #pragma once // 头文件内容
  3. 宏作用域控制

    c
    #undef TEMP_MACRO #define TEMP_MACRO(x) (x * 2) int result = TEMP_MACRO(5); #undef TEMP_MACRO

注意事项:

  1. 宏的副作用

    c
    #define SQUARE(x) ((x) * (x)) int i = 2; int result = SQUARE(i++); // 未定义行为
  2. 宏的括号

    c
    #define MUL(a, b) ((a) * (b)) int result = MUL(2 + 3, 4); // 正确: (2 + 3) * 4 = 20
  3. 条件编译的嵌套

    c
    #ifdef DEBUG #ifdef VERBOSE #define LOG(x) printf x #else #define LOG(x) #endif #else #define LOG(x) #endif
标签:C语言