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

是否存在两次包含相同标题实际上有用的情况?

为我的h/hpp文件创建标题保护对我来说一直是标准做法,但我想知道,为什么甚至可以两次包含相同的文件?是否存在实际需要不受保护的标头的情况?

c c++

8
推荐指数
3
解决办法
1133
查看次数

标签 统计

c ×2

c++ ×1

c-preprocessor ×1

enums ×1