NO

Author Topic: "Unreachable Code" error  (Read 173 times)

Offline noweare

  • Member
  • *
  • Posts: 4
"Unreachable Code" error
« 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;
}

Offline noweare

  • Member
  • *
  • Posts: 4
Re: "Unreachable Code" error
« Reply #1 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.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1672
Re: "Unreachable Code" error
« Reply #2 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;
« Last Edit: May 08, 2018, 12:28:36 pm by TimoVJL »
May the source be with you

Offline noweare

  • Member
  • *
  • Posts: 4
Re: "Unreachable Code" error
« Reply #3 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.

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 381
    • Alex's Link Sammlung
Re: "Unreachable Code" error
« Reply #4 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.
best regards
 Alex ;)

Offline noweare

  • Member
  • *
  • Posts: 4
Re: "Unreachable Code" error
« Reply #5 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