1
#include<iostream.h>
#include<conio.h>
#include<process.h>

void function(void);

int main(void)
{
 clrscr();

 int ch;

 while(1)
 {
  cin>>ch;

  if(ch==2)
    exit(0);
  else
    function();
 }//while
 return 0;
}//main

void function(void)
{
 cout<<"Hello";

 return;
}

The above code is working fine, but why I'm getting the "unreachable code" warning? I really don't understand what am I doing wrong. The compiler shows no warning when I comment/remove the return 0; statement in main(). Why is it so? Please tell me what is it that I'm doing wrong and what is the correct way to do it.

  • I was actually making a simple menu-driven program, consisting of a while loop, in which there is a switch case statement. I am a beginner, so please help. – Shashank Kadambri Sep 17 '18 at 16:01
  • 3
    There is no way the `return` statement can be executed. The execution can never get there. That's what "unreachable" means. – Cheers and hth. - Alf Sep 17 '18 at 16:03
  • 1
    You wil never leave the `while(1)` loop, so the `return 0;` will never be called. – Omagerio Sep 17 '18 at 16:03
  • 1
    Note: `` is a **pre-standard header**. Except if your institution requires you to use Turbo C++ (e.g. in a DOS box), you really need to upgrade your compiler. A modern compiler shouldn't have accepted that. – Cheers and hth. - Alf Sep 17 '18 at 16:04
  • 1
    "Why is this program showing "unreachable code" warning? And how do I suppress it?" - What you want to do is *fix* it. *Not* suppress it. – Jesper Juhl Sep 17 '18 at 16:45

2 Answers2

3

The while (1) loop has no option to leave.

Thereby, the exit(0) is not recognized as the dataflow analysis doesn't consider it as an option to jump to code behind of while (1) (and actually it doesn't).

Hence, there is no way to get to the return 0;.

If you replace exit(0) by a break than it changes. The break will cause to leave the while (1) and the return 0; becomes reachable.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • To be fair, `exit` not being recognized makes this a QoI issue. The standard library should be taken into account for such analysis. – StoryTeller - Unslander Monica Sep 17 '18 at 16:05
  • @StoryTeller Yepp. It will exit the `main()` but without jumping behind the `while(1)`. Hence my "actually it doesn't". So, it leaves the `return 0;` as unreachable what's true. – Scheff's Cat Sep 17 '18 at 16:06
3

why I'm getting the "unreachable code" warning? I really don't understand what am I doing wrong.

The loop has no return condition: while(1) and the loop body doesn't contain a break (or goto) that would jump out of the loop othwewise. Nevertheless, you have a return 0; statement after the loop. Since the execution never jumps out of the loop, it can never reach the return statement.

The compiler warns you that the line has no effect on the behaviour of the program, since the execution can never reach it. You can get rid of the warning by changing the logic of your program in a way that can potentially jump out of the loop. I suggest following:

if(ch==2)
  break;
else
  function();

The compiler shows no warning when I comment/remove the return 0; statement in main(). Why is it so?

That statement was the "unreachable code" that the warning referred to. If there is no unreachable code, then there is no need to warn about it.

It is safe to remove the line. main is special in the regard that it implicitly returns 0 in the absence of return statement (if the execution ever returns from main which your program never does).

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • "main is special in the regard that it implicitly returns 0 in the absence of return statement" - but then it shows a warning that "function should return a value"? @user2079303 – Shashank Kadambri Sep 17 '18 at 16:24
  • @ShashankKadambri • your compiler should not show a warning for that, for `main`. Unless your compiler is pre-standard. In which case I recommend getting a more current compiler. – Eljay Sep 17 '18 at 17:03
  • I know that Turbo C++ is a very old compiler, but we're asked to make programs using it only. Anyways, thanks for telling me that. @user2079303 – Shashank Kadambri Sep 17 '18 at 17:16
  • 1
    @ShashankKadambri indeed, my answer is correct only as far as standard C++ is concerned. I don't know much of turboc++, so I cannot dispute or confirm how it differs from the standard. – eerorika Sep 17 '18 at 17:40
  • 1
    @ShashankKadambri if you have to learn TurboC++, well, you do what you have to do to pass the class. But I strongly recommend supplementing your education by also learning modern C++. Your transition into the workforce will be smoother. – user4581301 Sep 17 '18 at 18:25