Pelles C forum
C language => Beginner questions => Topic started 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;
}
-
Its a warning not an error. I ran the code and the line was indeed able to be reached.
-
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;
-
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.
-
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.
-
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