Why write return 0 at the end?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrError
    New Member
    • Dec 2012
    • 11

    Why write return 0 at the end?

    Code:
    #include <iostream.h>
    #include <conio.h>
    int main ()
    {
        cout << "Hello World";
        getch();
        return 0;
    }
    In the above code, why is it necessary to write getch() and return 0? What is their purpose?
    Last edited by Banfa; Jan 22 '13, 04:26 PM. Reason: fixed the code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    the getch IMO is not at all necessary. People put it in because the do not understand the environment they are running there programs in and they run a command line program in a debug from there IDE. When run this way when the program exits the Window closes so they put in the getch so the program doesn't exit. If run not in debug mode or directly from Windows or from a command prompt, as a command line program is intended to be run, then the results remain on screen.

    The return 0 is there because main returns a int which is the program exit code and an exit code of 0 indicates success.

    In standard C++ this is not strictly required you can end main without a return statement and the return 0 is implicit, however since this code includes iostream.h which is not standard C++ (the standard header is iostream) I would have to assume you can't rely on it conforming to the standard so the return 0 is probably required.

    Comment

    • MrError
      New Member
      • Dec 2012
      • 11

      #3
      I couldn't understand this (not getting the technical terms like command line program.!):
      they run a command line program in a debug from there IDE. When run this way when the program exits the Window closes so they put in the getch so the program doesn't exit. If run not in debug mode or directly from Windows or from a command prompt, as a command line program is intended to be run, then the results remain on screen.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        getch() causes the program to pause at that point and wait for the user to press a key before the program proceeds to the return instruction and ends.

        This is a common idiom when the programmer is concerned that the output window (the one that shows "Hello World") will be automatically closed when the program ends. It has nothing to do with the desired function of the program. Do you need this line? I don't know -- you should comment it out and see what happens.

        By the way, notice that getch() and <conio.h> are not part of the language standard. Using them makes your program nonportable.

        Comment

        • swapnali143
          New Member
          • Mar 2012
          • 34

          #5
          as you are writing int main() that means it returns integer type of value so you need to return any integer value to main()
          return(0) means return true or return successfully..

          if you use void main() then dont use return(0)

          Your program would be as follows
          Code:
          #include <iostream.h>  
          #include <conio.h>
          void main ()
          {
              cout << "Hello World";
              getch();
          }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Yes of course void main() produces undefined behaviour and should be avoided. The only 2 definitions of main allowed by the C++ standard are

            int main()
            {
            }

            int main(int argc, char* argv[])
            {
            }

            Anything else, such as returning void, is undefined behaviour which is to be avoided at all costs.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Keep in mind that adding a getch() at the end of main() is usually for debugging purposes and is usually removed after the program is debugged.

              For that reason, the above discussion does not apply to Microsoft Visual Studio. If you use this product, you do not put a getch() at the end of main(). Instead, when you run the program you select "start without debugging" rather than "start debugging".

              Microsoft Visual Studio has two types of builds a "debug" build and a "release build". The difference is whether or not the compiler adds code to support the debugger in the executable.

              When you have a debug build and you "start without debugging", the execution ignores all of your debug breaks, maybe including the one you put at the end of main() to pause your program. So the execution is stopped at the end of
              main() with a "Press any key to continue..." added for your convenience.

              Comment

              • divideby0
                New Member
                • May 2012
                • 131

                #8
                It doesn't affect this program, but getch is unbuffered or at least that's been my experience with it. It doesn't wait on "enter" being pressed; if any key exists in the keyboard buffer, it will read and return its value right away.

                Code:
                while(cin.get() != '\n'){}
                Where "pausing" is actually desired, the above usually works for me, but my preference is to read all data into strings first and then convert as needed.

                Comment

                Working...