Thread: "Unreachable Code" error

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    33

    "Unreachable Code" error

    I get the above error. Compiler doesnt like the highlighted lines floor-0;
    below. My question is why would this be unreachable ?

    Code:
    #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;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I can't reproduce your problem. But here is what my compiler reports about that code:

    main.c||In function ‘main’:|
    main.c|6|warning: variable ‘floor’ set but not used [-Wunused-but-set-variable]|
    main.c|6|warning: unused variable ‘state’ [-Wunused-variable]|
    main.c|3|warning: unused parameter ‘argc’ [-Wunused-parameter]|
    main.c|3|warning: unused parameter ‘argv’ [-Wunused-parameter]|

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Actually it was a warning, not an error.
    So I ran the program and it was able to excute the line that was flagged in the warning.

    I am using Pelles compiler, so I posted on their forum.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > My question is why would this be unreachable ?
    Because your compiler isn't a stupid translator.

    It knows upBtn=0x02 has been set, hasn't changed when it gets to the problem line, and therefore knows that if(upBtn & 0x01) will always be false in this program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    I don't even know what 0x02 means or what an & does, but it looks like when you '&' a 0x01 and an 0x02 that you get 0, i.e. false, so your floor=0 assignment is skipped. Also, I'm guessing that you are doing some kind of bitwise arithmetic, but it looks like you are only covering four bits in your if else, and unsigned has more bits than that, so you are not covering all the possibilities. Hence you may reach the end of the program without having assigned a value to floor.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Thanks Salem, that makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printf error "address pointing at code space is taken"
    By KMAN999 in forum C Programming
    Replies: 2
    Last Post: 07-04-2011, 03:27 PM
  2. Can't Compile a code!!! I ran into "undefined reference to" error.
    By andereasmehdi in forum Linux Programming
    Replies: 19
    Last Post: 06-28-2011, 05:45 AM
  3. Replies: 9
    Last Post: 09-14-2010, 07:16 AM
  4. X server slaps some template code with a "fatal IO error"
    By Yarin in forum Linux Programming
    Replies: 8
    Last Post: 08-24-2009, 08:47 AM
  5. connect() returns "Network unreachable", but it isn't??
    By registering in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-22-2003, 02:09 PM

Tags for this Thread