Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将开关与宏一起使用_C_Switch Statement - Fatal编程技术网

C 将开关与宏一起使用

C 将开关与宏一起使用,c,switch-statement,C,Switch Statement,我试图在头文件(.h)中使用宏来模拟Switch语句 我有一些预定义的宏: #define MULTIPLY_BY_1 1 #define MULTIPLY_BY_10 2 #define MULTIPLY_BY_100 3 #define MULTIPLY_BY_1000 4 #define CHOSEN_FACTOR MULTIPLY_BY_100 我有一个常量结果,它根据选择的系数获取一个值(用户将定义此宏)。我在头文件中,我想“模拟”switch

我试图在头文件(.h)中使用宏来模拟Switch语句

我有一些预定义的宏:

#define MULTIPLY_BY_1      1
#define MULTIPLY_BY_10     2
#define MULTIPLY_BY_100    3
#define MULTIPLY_BY_1000   4

#define CHOSEN_FACTOR      MULTIPLY_BY_100
我有一个
常量结果
,它根据
选择的系数
获取一个值(用户将定义此宏)。我在头文件中,我想“模拟”switch语句,如下所示:

switch(CHOSEN_VALUE)
{
  case MULTIPLY_BY_1:
    const uint16_t result = 5;
    break;
  case MULTIPLY_BY_10:
    const uint16_t result = 50;
    break;
  case MULTIPLY_BY_100:
    const uint16_t result = 500;
    break;
  case MULTIPLY_BY_1000:
    const uint16_t result = 50000;
    break;

  default:
    break;
}
uint16_t foo(void)
{
  uint16_t myFoo = getMyFooValue();
  return result * myFoo;
}
#define INIT_FACTOR(var, value)               \
  #ifdef CHOSEN_FACTOR                        \         
    #if CHOSEN_FACTOR == MULTIPLY_BY_1        \
      const uint16_t var = value              \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_10     \
      const uint16_t  var = value * 10        \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_100    \
      const uint16_t var = value * 100        \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_1000   \
      const uint16_t var = value * 1000       \
    #endif                                    \
  #else                                       \
    const uint16_t var = value                \
  #endif                                      
编辑:

在源文件(.c)中,我希望使用如下结果:

switch(CHOSEN_VALUE)
{
  case MULTIPLY_BY_1:
    const uint16_t result = 5;
    break;
  case MULTIPLY_BY_10:
    const uint16_t result = 50;
    break;
  case MULTIPLY_BY_100:
    const uint16_t result = 500;
    break;
  case MULTIPLY_BY_1000:
    const uint16_t result = 50000;
    break;

  default:
    break;
}
uint16_t foo(void)
{
  uint16_t myFoo = getMyFooValue();
  return result * myFoo;
}
#define INIT_FACTOR(var, value)               \
  #ifdef CHOSEN_FACTOR                        \         
    #if CHOSEN_FACTOR == MULTIPLY_BY_1        \
      const uint16_t var = value              \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_10     \
      const uint16_t  var = value * 10        \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_100    \
      const uint16_t var = value * 100        \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_1000   \
      const uint16_t var = value * 1000       \
    #endif                                    \
  #else                                       \
    const uint16_t var = value                \
  #endif                                      
有没有基于宏的解决方案?是否有更优化的方法来获得相同的结果

与answer建议一样,我建议您使用enum获取所选值,并根据enum类型的选择返回指定的宏。为此,您可以创建一个函数并获得正确的宏,如下所示

typedef enum {
    MULTIPLY_BY_1
    MULTIPLY_BY_10     
    MULTIPLY_BY_100    
    MULTIPLY_BY_1000   
}multiplier_t;

uint16_t foo(multiplier_t multiplier)
{
       switch (multiplier) {                      
        case MULTIPLY_BY_1:           
           return 1;       
        case MULTIPLY_BY_10:       
          return 2;   
        case MULTIPLY_BY_100:       
          return 3;
        case MULTIPLY_BY_1000:       
          return 4;    
     }            
    return 0;  // just in case no code matches
}

希望这对您有所帮助。

假设我理解您的要求,我想您正在寻找这样的东西:

switch(CHOSEN_VALUE)
{
  case MULTIPLY_BY_1:
    const uint16_t result = 5;
    break;
  case MULTIPLY_BY_10:
    const uint16_t result = 50;
    break;
  case MULTIPLY_BY_100:
    const uint16_t result = 500;
    break;
  case MULTIPLY_BY_1000:
    const uint16_t result = 50000;
    break;

  default:
    break;
}
uint16_t foo(void)
{
  uint16_t myFoo = getMyFooValue();
  return result * myFoo;
}
#define INIT_FACTOR(var, value)               \
  #ifdef CHOSEN_FACTOR                        \         
    #if CHOSEN_FACTOR == MULTIPLY_BY_1        \
      const uint16_t var = value              \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_10     \
      const uint16_t  var = value * 10        \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_100    \
      const uint16_t var = value * 100        \
    #elif CHOSEN_FACTOR == MULTIPLY_BY_1000   \
      const uint16_t var = value * 1000       \
    #endif                                    \
  #else                                       \
    const uint16_t var = value                \
  #endif                                      
这将定义一个名为
INIT_FACTOR
的宏,该宏包含两个参数,即要定义的变量的名称和起始值。您可以将其作为

INIT_FACTOR(result, 5);
然后,如果
选择的\u因子
乘以\u 100
,则该行将扩展为

const uint16_t result = 5 * 100;
如果未定义所选的系数,则该行扩展为

const uint16_t result = 5;

请记住,宏替换发生在编译时,而不是运行时。如果您想要运行时解决方案,这不是它

在预处理器中进行选择时,可以使用辅助宏展开参数,然后使用标记将其粘贴到查找表中:

#include <stdio.h>


int main(void)
{
    #define MULTIPLY_BY_1      1
    #define MULTIPLY_BY_10     2
    #define MULTIPLY_BY_100    3
    #define MULTIPLY_BY_1000   4

    #define CHOSEN_FACTOR      MULTIPLY_BY_100

    #define Foo1    1
    #define Foo2    10
    #define Foo3    100
    #define Foo4    1000
    #define FooHelper(x)    Foo##x
    #define Foo(x)  FooHelper(x)

    printf("%d\n", Foo(CHOSEN_FACTOR));
}
#包括
内部主(空)
{
#定义乘以1
#定义乘以2
#定义乘以3
#定义乘以4
#定义所选的系数乘以100
#定义Foo1 1
#定义Foo2 10
#定义Foo3 100
#定义Foo4 1000
#定义fooohelper(x)Foo##x
#定义Foo(x)fooohelper(x)
printf(“%d\n”,Foo(选择的因子));
}

通常应避免此类预处理器滥用,对于引发此问题的实际问题可能不需要此类预处理器。

谢谢大家的回答。我在寻找一个具体的解决方案,我想我找到了

#define MULTIPLY_BY_1      0
#define MULTIPLY_BY_10     1
#define MULTIPLY_BY_100    2
#define MULTIPLY_BY_1000   3

const struct
{
  uint8_t index;
  uint16_t value;
}myArray[] = {
  {MULTIPLY_BY_1, 1},
  {MULTIPLY_BY_10, 10},
  {MULTIPLY_BY_100, 100},
  {MULTIPLY_BY_1000, 1000}
};

#define CHOSEN_VALUE MULTIPLY_BY_10

const uint16_t result = myArray[CHOSEN_VALUE].value;

void foo(void)
{
   printf("%d", result); // 10
}

您计划如何使用所选的值?请给我们举个例子。你知道在
C99
之后,
const result=5无效,对吗?您需要显式
const int result=5,因为隐式整数规则已过时。@user694733 edited您不能定义
乘以10
等等吗?然后它减少到
5*选择的_因子
。另外,为什么不直接定义
#定义乘以_100050000
?临时值
4
有什么好处?