相关疑难解决方法(0)

在C中使用枚举类型的变量作为字符串的简单方法?

这是我想要做的:

typedef enum { ONE, TWO, THREE } Numbers;
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个函数来执行类似于以下的切换案例:

char num_str[10];
int process_numbers_str(Numbers num) {
  switch(num) {
    case ONE:
    case TWO:
    case THREE:
    {
      strcpy(num_str, num); //some way to get the symbolic constant name in here?
    } break;
    default:
      return 0; //no match
  return 1;
}
Run Code Online (Sandbox Code Playgroud)

而不是在每种情况下定义,有没有办法使用枚举变量设置它,就像我在上面尝试做的那样?

c enums c-preprocessor

86
推荐指数
8
解决办法
9万
查看次数

标签 统计

c ×1

c-preprocessor ×1

enums ×1