The Wayback Machine - https://web.archive.org/web/20241123012729/https://www.geeksforgeeks.org/cpp-program-to-show-unreachable-code-error/
Open In App

C++ Program to Show Unreachable Code Error

Last Updated : 25 Jul, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Unreachable code is a compile-time error in languages like C++, Java, and C or Unreachable code error occurs when the code can’t be compiled; but why it is just a warning in C++ & C ? Warnings are a way that the compiler indicates that the particular mentioned thing might become an error in future.

Example:

C++




// C++ Program to demonstrate
// Unreacheable Code error
#include <iostream>
using namespace std;
  
int c();
int main()
{
    cout << c() << endl;
    return 0;
}
int c()
{
    int a = 3;
    return a; // oops it is unreachable code
    int b = 6;
    cout << b; // program control never goes here
}


Output

3


Similar Reads

C++ Program to Show Thread Interface and Memory Consistency Errors
C++ allows Multithreading by using the 'thread' header file. The program acts as one thread but to increase program execution time/performance we can use threads to run parts of the program concurrently. But it may lead to issues of memory consistency errors and may not give us the proper output. Threads are used to improve the performance of appli
2 min read
C++ Program to Show Use of This Keyword in Class
Here, we will see how to use this keyword in a class using a C++ program. this keyword in C++ is an implicit pointer that points to the object of the class of which the member function is called. Every object has its own this pointer. Every object can reference itself by this pointer. There are 4 ways this keyword can be used in a class in C++: Res
4 min read
C++ Program to Show Types of Errors
In any programming language errors is common. If we miss any syntax like parenthesis or semicolon then we get syntax errors. Apart from this we also get run time errors during the execution of code. In a similar way the errors are classified as below: Syntax Errors Runtime Errors Logical Errors Linked Errors Semantic Errors 1. Syntax ErrorsThese ar
3 min read
C++ Program to Show Runtime Exceptions
A runtime error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully. Here, we will learn how to handle runtime exceptions in C++. There are 5 types of runtime exceptions discussed here: Division by zero. Segmentation faults. Large memory allocation/Large Static Memory Allocati
3 min read
How to create the boilerplate code in VS Code?
While programming, there is often a piece of code that needs to be written repetitively. For example, loops (for loop, while loop, do-while loop), or classes and functions. To write such a piece of code, again and again, becomes a daunting task, and often copy and paste is done, which definitely takes a lot of time and effort. The above problem can
2 min read
How to Hide and Show a Console Window in C++?
The task is to hide and Show the console window of a C++ program. The program for the same is given below. Note: The results of the following program can only be seen when it is executed on a console. Example: C/C++ Code // C++ program to hide and show a console window #include <iostream> #include <windows.h> using namespace std; void c
2 min read
Convert C/C++ program to Preprocessor code
We use g++ compiler to turn provided Cpp code into preprocessor code. To see the preprocessor code generated by the CPP compiler, we can use the “-E” option on the command line: Preprocessor include all the # directives in the code. and also expands MACRO function. Syntax: g++ -E filename.cpp // MACRO named MAX initialism to 10 #define MAX 10 int m
1 min read
C++ Program to Determine the Unicode Code Point at a Given Index
Here, we will find out the unicode code point at a given index using a C++ program. Input: arr = "geEKs" Output: The Unicode Code Point At 0 is = 71. The Unicode Code Point At 1 is = 101. The Unicode Code Point At 2 is = 69. The Unicode Code Point At 3 is = 107. The Unicode Code Point At 4 is = 83.Approach: If the array/string value is increased th
2 min read
Build a C++ Program that Have Multiple Source Code Files
In C++ generally, we saw only a single file program but what if we have multiple source code files and we have to build a program by using all these files? Let's see how we can build a C++ program with multiple source files without including them as header files. Use two or more source code files in C++ There are two methods to use two or more sour
3 min read
error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Two or more functions are said to be overloaded if they differ in any of th
9 min read
cerr - Standard Error Stream Object in C++
Standard output stream(cout): cout is the instance of the ostream class. cout is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream(cout) using the insertion operator(<<). Standard error stream (cerr): cerr is the sta
2 min read
How to fix auto keyword error in Dev-C++
The auto keyword in C++ specifies that the type of the variable that is being declared will be automatically deducted from its initializer. In case of functions, if their return type is auto then that will be evaluated by return type expression at runtime. // C++ program to illustrate the auto // keyword in DevC++ compiler #include <bits/stdc++.
2 min read
Expression must have class type error in C++
The expression must have class type is an error that is thrown when the dot(.) operator is used to access an object's properties, is used on pointers to objects. Dot('.') Operator is basically used for accessing the fields and methods of an object, but when a dot operator is used on the pointer to object in that condition, the error "Expression mus
2 min read
Reason of runtime error in C/C++
In this article, we will discuss the reason for the run-time error and its solution. Runtime Error: A runtime error in a program is an error that occurs while the program is running after being successfully compiled. Below are some methods to identify the reason behind Runtime errors: Method 1: When the index of the array is assigned with a negativ
3 min read
Substitution Failure Is Not An Error (SFINAE) in C++
Prerequisite: Templates in C++ Substitution Failure Is Not An Error (SFINAE) is a principle of the C++ programming language that states that a compiler should not fail to compile a program just because it cannot substitute a template parameter. This principle allows for the use of template metaprogramming and enables the compiler to make decisions
4 min read
Memory Limit Exceeded Error
Memory Limit Exceeded Error: It typically occurs when no memory limit has been set. It means that the program is trying to allocate more memory than the memory limit for the particular problem. For Example, if the memory limit is 256 MB, then there is no need to write code that requires more than 256 MB of memory. Generally, all online platforms ha
3 min read
C++ Error - Does not name a type
In C++, one of the most common errors that programmers encounter is the “does not name a type” error. The error usually occurs when the programmer writes a piece of code in which the compiler is not able to recognize the type or definition for it. In this article, we are going to explore the “does not name a type” error in C++ in detail, including
6 min read
"Access is Denied error" in C++ Visual Studio and How to resolve it
Pre-requisite: How to Install Visual C++ on Windows In this article, we will discuss about access denied error in Visual Studio in C++. What is Access denied error in Visual Studio in C++? Access denied error in Visual Studio in C++ occurs when there is an attempt to access one of the installation files and due to some reason the access is denied t
3 min read
Map of Vector Struct and Struct Giving Error in C++ STL
Prerequisites: Structure in C++Vector in C++ A structure is a user-defined data type that groups together related data items of different data types. Structures are similar to classes, but they have some key differences. A vector is a container object that can store a sequence of values. It is similar to an array, but it can grow and shrink dynamic
5 min read
Expected Unqualified Id Error in C++
Syntax in C++ plays a vital role and even with a slight mistake, it can give birth to a lot of unexpected errors. One of these errors is the "expected unqualified id error" that can arise due to some common oversights while writing your code. In this article, we are going to dive deep into the expected unqualified id error and what can be its possi
6 min read
erf | Error functions using cmath in C++
In mathematics, the error function (also called the Gauss error function) is a special function (non-elementary) of sigmoid shape that occurs in probability, statistics, and partial differential equations describing diffusion. It's the probability that a normal random variable with mean 0 and variance 0.5 take value between [-x, x].  It is denoted
2 min read
How to Fix Undefined Reference Error in C++?
In C++, the undefined reference error typically occurs during the linking phase of compilation. This error indicates that the compiler knows about a function or variable (it has been declared), but it cannot find the actual implementation (definition) during the linking process. The linker needs to resolve these references to create a complete exec
2 min read
What is an Undefined Reference Error in C++?
In C++, an undefined reference is an error that occurs when the linker can't find the definition of a function or a variable used in the linking stage of the compilation process. This error needs to be resolved for the smooth working of the program. When Does Undefined Reference Error Occur in C++?An undefined reference error is a linker error, not
2 min read
How to Get Error Message When ifstream Open Fails in C++?
In C++, the std::ifstream class is used to open a file for input operations. It associates an input stream to the file. However, due to some reasons, it may be unable to open the requested file. In this article, we will learn how to show an error message when the ifstream class fails to open the file. Get Error Message When ifstream Open Fails in C
2 min read
How to Output Error When Input Isn't a Number in C++?
In C++, when taking user input, we may need to validate that it is a number and output an error if it is not. In this article, we will learn how to output an error when the input isn't a number in C++. For Example, Input:Enter a Number: GOutput:Error: That was not a number.Output Error When Input Isn't a Number in C++The std::cin automatically dedu
2 min read
How Can I Solve the Error LNK2019 Unresolved External Symbol?
Errors are typical while working with C++ programs, and LNK2019: Unresolved External Symbol - Function is one particularly such annoying issue that occurs when the declared and used function or variable in our code is not found by the linker. In this article, we will learn how to solve the Error LNK2019 in C++. Solving the Error LNK2019: Unresolved
3 min read
Error: std::endl is of Unknown Type when Overloading Operator <<
In C++, the << operator is commonly overloaded for custom output for user-defined types using streams. However, an issue may arise when using std::endl with an overloaded operator<<. It is shown as the type of std::endl being unknown, leading to compilation errors. In this article, we will learn the cause of this issue and how can we re
4 min read
Syntax Error in C++
Syntax plays a vital role and even a slight mistake can cause a lot of unexpected errors. One of these errors is the "expected unqualified id error" or "expected unqualified id before ' ' token" which can arise due to some common oversights while writing your code. In this article, we are going to dive deep into the expected unqualified id error an
6 min read
Rearrange the given source code
Given a string str which contains the source code (C/C++/Java code) which is not arranged in a proper way, the task is to format that code in a proper way and print the result. Examples: Input: str = "#include<stdio.h>int main(){ cout << \"geeksforgeeks\" }" Output: #include<stdio.h> int main() { cout << "geeksforgeeks" } In
9 min read
Code valid in both C and C++ but produce different output
There are some syntactical structures that are valid for both C and C++ but different behavior when compiled and run in the both languages. Several differences can also be exploited to create code that compile in both languages but behave differently. For example, the following function will return different values in C and C++: Example Codes valid
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg