相关疑难解决方法(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万
查看次数

极少数情况下必须使用MACRO

调试宏可能需要很长时间.我们最好避免使用它们,除非在非常罕见的情况下,常量,函数和模板都不能达到我们想要的效果.

什么是罕见的情况?

c c++ macros

26
推荐指数
5
解决办法
3205
查看次数

标签 统计

c ×2

c++ ×1

c-preprocessor ×1

enums ×1

macros ×1