MPLAB XIDE的使用问题

[复制链接]
13481|5
 楼主| flycamelaaa 发表于 2021-9-16 15:55 | 显示全部楼层 |阅读模式
IDE, mplab, se, ce, AC, tc
MPLAB X IDE里面,switch语句在case后面不能使用变量吗
switch(Displacement[5])
    {
        case Displacement[0]:
            Run_Section = 0;//加加速段
        break;

像上面这样,报错如下
Interrupt.c:297:9: error: case label does not reduce to an integer constant
         case Displacement[0]:
我记得有些编译器都可以这样用的呀,MPLAB不允许这样吗
laocuo1142 发表于 2021-9-16 15:55 | 显示全部楼层
case 后面跟变量不行吧,跟逻辑条件是可以的.
powerantone 发表于 2021-9-16 15:56 | 显示全部楼层

印象中case是不可以用变量,只能用整形数进行判断,这样编译器可以产生高效的优化代码,否则不如直接用 if-else了。
stormwind123 发表于 2021-9-16 15:56 | 显示全部楼层

用常量
cyclefly 发表于 2021-9-16 19:37 | 显示全部楼层
貌似不能
lss1330 发表于 2021-11-2 21:58 | 显示全部楼层
  1. switch(Displacement[5])
  2.     {
  3.         case Displacement[0]: {
  4.             int xxx = 0;
  5.             Run_Section = 0;//加加速段
  6.         }break;


在冒号后边加花括号“{}”
您需要登录后才可以回帖 登录 | 注册

本版积分规则

837

主题

4432

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部