您可以将枚举用于 gastypes,将描述字符串数组用于可打印表示。将结果状态存储为整数/枚举的优点是可以很容易地进行比较,例如,在 a 中使用switch。相比之下,比较字符串有点麻烦。
这是使用X-Macros的示例实现:
#include <stdio.h>
#define GASTYPES \
    ENTRY(MONOATOMIC) \
    ENTRY(DIATOMIC) \
    ENTRY(POLYATOMIC)
typedef enum {
#define ENTRY(x) x,
    GASTYPES
#undef ENTRY
} gastype_t;
const char const * gastype_str[] = {
#define ENTRY(x) #x,
    GASTYPES
#undef ENTRY
};
int main() {
    double gamma; 
    gastype_t gastype;
    if(scanf("%lf", &gamma)) {
        if (gamma <= 1.36)
            gastype = POLYATOMIC;
        else if (gamma <= 1.5)
            gastype = DIATOMIC;
        else
            gastype = MONOATOMIC;
        printf("This gas is %s\n", gastype_str[gastype]);
        return 0;
    }
    else {
        printf("Failed to parse input :(\n");
        return -1;
    }
}
因此,在实际编译之前,预处理器将枚举和描述字符串数组的定义扩展为以下内容:
typedef enum {
    MONOATOMIC,
    DIATOMIC,
    POLYATOMIC,
} gastype_t;
const char const * gastype_str[] = {
    "MONOATOMIC",
    "DIATOMIC",
    "POLYATOMIC",
};
使用示例:
$ gcc test.c && echo "1.4" | ./a.out
This gas is DIATOMIC