Skip to content
Related Articles

Related Articles

C Program to Show Unreachable Code Error

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Jul, 2022

Here, we will see how to show unreachable code errors using the C program. Unreachable statements are statements that will not be executed during the program execution. These statements may be unreachable due to the following reasons:

  1. Return statement before print statement.
  2. Infinite loop before statements. 
  3. Statements after the continue statements. 
  4. False conditions in if statements.

Let’s start discussing each of these scenarios in detail.

1. Return Statement Before Print Statement

When a return statement is executed it will stop the execution of the program so the second printf statement will not be executed due to an unreachable code error. In the below example it can be observed that the first printf statement will print the output but after that, there is the return statement. 

Below is the C program to demonstrate unreachable code error when a return statement is present before print statement:

C




// C program to demonstrate 
// unreachable code error
#include <stdio.h>
  
// Driver code
int main() 
{
    printf("geeks for geeks"); 
    return 0; 
    printf("gfg");
}

Output: 

geeks for geeks 
prog.c: error: unreachable statement 
printf("gfg")

2. Infinite loop before statements: In the below C program, even though the value of a is equal to 2 it will not print the output because of the break statement. The break statement will stop the execution and coming out of the loop.

C




// C program to implement
// the above approach
#include <stdio.h>
  
// Driver code
int main() 
{
  int a = 2;
  for (;;) 
  {
    if (a == 2)
    {
      break;
        
      // It will never execute, so
      // same error will be there. 
      printf("GeeksforGeeks"); 
    
  }
  return 0;
}

Output: 

prog.c: error: unreachable statement 
printf("GeeksforGeeks"); 

3. Statements after continue statements: The below code shows the example of the continue statement. In the code, there is a continue statement, the continue statement transfers the control to the beginning of the loop. So it will not execute the statements below the continue statement. 

C




// C program to implement
// the above approach
#include <stdio.h>
  
// Driver code
int main() 
{
  for (int i = 0; i < 5; i++)
  {
    continue;
    printf("GeeksforGeeks");
  }
  return 0;
}

Output: 

prog.c: error: unreachable statement
printf("GeeksforGeeks"); 

4. False conditions in if statements:

Example1:  The value of sqrt(2) will be 1.414. if the statement checks the condition, the value of X is not greater than 5 so it will not be entered inside the if statement and will not execute the X++ statement.

C




// C program to show the 
// unreachable error
#include <math.h>
#include <stdio.h>
  
// Driver code
int main()
{
  // The value of sqrt(2) is 1.414
  double X = sqrt(2);
    
  // 1.414>5 condition false so it 
  // will not be entered inside the 
  // if statement
  if (X > 5) 
  {
    /* unreachable code */
    X++;
  }
  return 0;
}

Output: 

prog.c: error: unreachable statement
X++; 

Example 2: In the below code the printf statement will never be executed because the value of N will be 3 and which is never equal to 4. So the if statement will not be entered into the inside block of if.

C




// C program to show the 
// unreachable error
#include <stdio.h>
  
// Driver code
int main()
{
  // The value of N is 3
  int N = 2 + 1;
    
  // 3==4 false so it will not be 
  // entered inside the if statement
  if (N == 4) 
  {
    /* unreachable code */
    printf("equal");
  }
  return 0;
}

Output:  

prog.c: error: unreachable statement
printf("equal");   

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!