Pelles C forum

C language => Beginner questions => Topic started by: noweare on May 05, 2018, 12:42:12 AM

Title: "Unreachable Code" error
Post by: noweare on May 05, 2018, 12:42:12 AM
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;
}
Title: Re: "Unreachable Code" error
Post by: noweare on May 05, 2018, 02:15:26 AM
Its a warning not an error. I ran the code and the line was indeed able to be reached.
Title: Re: "Unreachable Code" error
Post by: TimoVJL on May 05, 2018, 09:20:27 AM
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
Code: [Select]
volatile unsigned int upBtn = 0x02;
Title: Re: "Unreachable Code" error
Post by: noweare on May 08, 2018, 03:38:17 AM
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.
Title: Re: "Unreachable Code" error
Post by: AlexN on May 08, 2018, 08:01:04 AM
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.
Title: Re: "Unreachable Code" error
Post by: noweare on May 11, 2018, 01:21:11 PM
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