Thread: Beginner question

  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
    Chappell Hill, Texas
    Posts
    2,332
    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".
    Mainframe assembler programmer by trade. C coder when I can.

  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,366
    >> 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 07: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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07: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, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07: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