"Unreachable Code" error

Started by noweare, May 05, 2018, 12:42:12 AM

Previous topic - Next topic

noweare

I am getting the above error on the blue code below.
I don't know why this would happen, looks ok to me.
Sorry for not using code tags but the editor wouldnt let me highlight within the code tage


#include <stdio.h>

int main(int argc, char *argv[])
{
unsigned int upBtn=0x02; 
unsigned int state=2, floor;

    if (upBtn > 0){

      if(upBtn & 0x01){
            floor=0;
      }
   
      else if(upBtn & 0x02){
            floor=1;
      }
   
      else if(upBtn & 0x04){
            floor=2;
      }
   
      else if(upBtn & 0x08){
            floor=3;
      }
   }


    return 0;
}

noweare

Its a warning not an error. I ran the code and the line was indeed able to be reached.

TimoVJL

#2
You get warning test1.c(11): warning #2154: Unreachable code. from optimized code, as optimizer removes unused code.
Compile it without optimizations to see difference.

EDIT: for testing volatile unsigned int upBtn = 0x02;
May the source be with you

noweare

Yes that worked. That was just a piece of a larger program so upBtn is shown not to change to test something out.
Thank you.

AlexN

In your example the blue line is uneachable because the locale variable upBtn is defined with the constant value 0x02. So all pathes exept with upBtn & 0x02 are unreachable. If you make upBtn global, the unreachable vanish.
best regards
Alex ;)

noweare

Right, there is no change in that variable floor will always be equal to 1, evaluation of the if statements would not be necessary.
Thanks AlexN