int main and void main

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • greek
    New Member
    • Nov 2006
    • 17

    int main and void main

    Hi all..i wud b grateful if u cud explain to me when to use int main and void main, wat is the difference and when to use return 0 or 1 in c++..thx
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    In C++, you can NEVER use a void main(). It must always be int main().

    in C++, returning a 1 inside main indicates that there was an error. A common occurrence of this is when you are trying to input from a file, but the file cannot be loaded. It correctly exits the program, but with the added note that there was some error. Returning 0 indicates that everything worked just as planned.

    Comment

    • iWillLiveforever
      New Member
      • Feb 2007
      • 136

      #3
      actually you can use void main in C++ it just does not return anything i used it all the time in Turbo C++ if you do use it make sure to put a getch() at the end so the program pauses for the user to press a key

      Comment

      • RRick
        Recognized Expert Contributor
        • Feb 2007
        • 463

        #4
        In the C world, you can use either the void or int format. Some C compilers issue a warning about the void main, but still compile the program.

        The C++ world took a hard line approach to main. Now you get an error if you don't use int, end of story. I was surprised when I first saw it. I don't know why they took this hard line approach. Good intentions to force good coding? Who knows.

        There is a 3rd approach: Don't define a return value for main, at all. The gnu c++ compiler spits out a warning but will compile it.

        Comment

        • iWillLiveforever
          New Member
          • Feb 2007
          • 136

          #5
          no you can use void main in some compilers i don't know about the new TC but in the old dos Turbo C++ you can use void main

          Comment

          Working...