Beginner question

This is a discussion on Beginner question within the C++ Programming forums, part of the General Programming Boards category; What's the difference between int main() and void main()?...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    Beginner question

    What's the difference between int main() and void main()?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    The type prior to main tells the compiler what type of return value the function will be returning.

    In the case of the special function "main", it should always return a value to the caller to let the caller know the success or failure of the process.

    "int" says an integer will be returned, commonly called a "return code".

    "void" says no value will be returned.

    "void main" should be avoided. It's "old school".
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    62
    Any case where a beginner might want to use void main() or main(void) (difference?) instead of int main()

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Void main is wrong and indeed has almost never been right (though I remember a compiler I used for an embedded machine required void main()).

    One out of every five times you use int main() you will find a dollar. Every time you use void main() you will drop a dollar for an int main() user to find... Sorry... rules are rules.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Any case where a beginner might want to use void main() or main(void) (difference?) instead of int main()

    http://www.research.att.com/~bs/bs_faq2.html#void-main

    If you're not sure, use int main.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Glauber,

    The programing language standard says that main() should return an int. It's one of the many rules in C++.

    Have you studied functions* yet? When you study functions, you will learn the difference between void SomeFunction() and int SomeFunction()

    When your program calls a function, the function can return a value (of a particular type) when the function is done. A return-type of void in the function prototype indicates that no value is returned. Otherwise, the return value can be the result of a calculation (done by the function) or it can be a pass/fail flag, or anything else.

    In the case of main(), you may be able to "get away" with the incorrect practice of returning nothing (if your compiler allows it), because main() doesn't return it's value 'till your program ends... So, by the time the operating system sees the error, your program is no longer running...



    * The cprogramming.com tutorial just scratches the surface. You'll need a book (or other resources) to really learn about functions.
    Last edited by DougDbug; 05-27-2008 at 08:29 PM.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    53
    There is one "special" thing about main() (in C99), in that if the closing } is reached without ever seeing a return statement, 0 is returned.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    There is one "special" thing about main() (in C99), in that if the closing } is reached without ever seeing a return statement, 0 is returned.
    This is C++ though, so C99 does not matter, but it also happens that C++ has the same rule.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    62
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 08:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 03:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 08:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21