Thread: main and return question

  1. #1
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89

    main and return question

    Just a quicky.

    I'm a newcomer to C and obviously want to pick up as few bad habits as I can. One thing that I would like some input on is the correct use of "main()" and "return".

    Some texts I am using code main as:

    Code:
    main()
    while others show:

    Code:
    int main()
    the same confusion I have is with return:

    Code:
    return 0;
    or

    Code:
    return (0);
    which is correct or does it not matter?

    Cheers all

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    it doesnt matter it will return 0 in the end

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lolguy View Post
    it doesnt matter it will return 0 in the end
    Well, as long as there IS a return 0 at the end, yes.

    Code:
    return (0);
    is identical to
    Code:
    return 0;
    or indeed:
    Code:
    return (((((((0)))))));

    The forms of:
    Code:
    main()
    (called implicit return type) and
    Code:
    int main()
    (explicit return type) are also identical, except the former is only valid in the older C89 standard - the newer C99 standard means that "implicit return type" is no longer valid.

    It is highly recommended to use the modern version when writing new code, but it is also good to be aware that old-style code is out and is valid in existing C89 compilers (most compilers have an option to support C89 style code, if that's not the default).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    Well, as long as there IS a return 0 at the end, yes.
    Though in C99, 0 will be returned if control reaches the end of the main function without encountering a return statement.
    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

  6. #6
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Thanks for the responses all.

    Confusion cleared.

    Quote Originally Posted by laserlight View Post
    Though in C99, 0 will be returned if control reaches the end of the main function without encountering a return statement.
    ^^ arrgh! My brains!

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BuzzBuzz View Post
    ^^ arrgh! My brains!
    Yes, it's hard to decide what is relevant information and to whom, especially if nobody else knows either.

    In this context, just consider the difference between something that is okay somewhere, and something that is okay everywhere.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BuzzBuzz View Post
    ^^ arrgh! My brains!
    If you ALWAYS have a return 0, you never need to remember whether it is needed or not - it is NEVER WRONG to end main with a return 0 - it CAN be right or wrong to NOT do so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Unless it's unreachable code - some function calls "exit(n)" legitimately. Compiler may yell at you. Oh well.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nonoob View Post
    Unless it's unreachable code - some function calls "exit(n)" legitimately. Compiler may yell at you. Oh well.
    Yeah, obviously there are cases where you never leave main with a return, in which case putting a return 0 will have no negative or positive effect on the code at all. A good quality optimizer in the compiler will remove unreachable code. If you have a poor quality optimizer in the compiler, it may not remove unreachable code - but it's almost certainly a bigger problem with other functions than main.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM