Thread: Why do people still use main() or main(void)?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    14

    Why do people still use main() or main(void)?

    If the standard today is to write : int main(void) or int main(int argc, char *argv[]). Return 0 is implied under the C99 standard so some people ommit it but it's supposed to be good programming to include it. Maybe ommitting the return 0; reduces program size and speeds up the execution of the program, although the speed up is obviously negligable.

    So why do people continue to use main() and main(void)? without int

    If I started a program with main() and left out the return 0; it'll work okay right? and i won't run into any problems under the C99 environment?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    There's an FAQ entry on this :

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    And main always returns an int. Always. Therefore, you always put in the return 0.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    14
    I already read that. Main returning an int doesn't mean i need to write int main() right?

    And a lot of people leave out return 0; unless they're compinling under the C89 environment which requires it to be there.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If I started a program with main() and left out the return 0; it'll work okay right?
    Only under C99. Anything earlier is undefined behavior. So it's probably wiser to always return something explicitly.

    >So why do people continue to use main() and main(void)? without int
    Because they don't know better and/or haven't been burned by such things in the past.
    My best code is written with the delete key.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Return 0 is implied under the C99 standard so some people ommit it
    As you say, it's good practice to say what you mean.

    IMO, the standards committees made main() special in this way (no other int returning function implies return 0) as a sop to all the void main programmers to enable them to get their programs up to standard by only changing one word.

    I also find it ironic that for the past 20 years the standards committees have been going out of their way to remove implicit behaviour, and now they're adding some other implicit behaviour.

    > Maybe ommitting the return 0; reduces program size and speeds up the execution of the program
    Well the only thing I see being saved is 10 characters of source code.
    The performance "gain" of omitting one instruction which happens only once in the life of a program is truly astonishing compared to the risk of undefined behaviour, and all the work the program has to do to get to that point.

    > If I started a program with main() and left out the return 0; it'll work okay right?
    Only if you're sure of your environment.
    Personally, I go with a style which works everywhere regardless of compiler or standards committee fudges and say that main returns int, and there is an explicit return 0; at the end. Works every time, and does exactly what it says on the tin.

    Then I can concentrate on the vastness of what the program is actually all about rather than worrying about some minor bit of triviality which can only cause grief by trying to second-guess what the compiler may or may not allow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just looking at your avatar animation, I would have NEVER guessed you weren't a fan of void main() !!

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The only standardized version of main() that works for sure on all compilers, over C and C++, is supposed to be:

    Code:
    int main(int argc, char *argv[]);
    From my understand that C++ doesn't support void as the argument type, and C99 doesn't support empty args (since they mean different things in C and C++).

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    C++ does allow "void" as the argument type, it's just discouraged. (I'm not sure if it's deprecated or not.)

  9. #9
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Unhappy What does it mean when a function returns an integer?

    Some functions return an integer like int main( ) and some of them return void ( I mean nothing) what does it mean when it returns an integer and something like void and what are the arguments of the function?


    Please reply!

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Having a function that returns an int means that the function provides an integer for the calling function to read. A return of void means that the calling function is not to expect a return of any variable.

    Code:
    int add(int x, int y)
    {
    	return (x + y);
    }
    The above function takes in two integers, and returns one back.

    In this case, nothing is explicitly returned:

    Code:
    void printerror(char *szError)
    {
    	fprintf(stderr,"The following error has occurred: %s\n",szError);
    }
    In the special case of main(), I wrote a post on this with regard to the x86 processor and had an int will be returned anyway even if you mark it as returning void. This actually applies to all void functions, because the method of "returning" a variable is sort of ambiguous and is really meaning that a variable will be placed in some known location so that the calling function can locate it. This location may be overwritten in the case of a void function, and appear to explicitly return such a value. Overall, this is an advanced topic, that beginners should not bother with. All they should know is that they should declare main() to return an int, and accept the proper parameters.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    because the method of "returning" a variable is sort of ambiguous and is really meaning that a variable will be placed in some known location so that the calling function can locate it.
    It's not ambiguous at all and is quite simple to follow for x86 architecture.

    integers - returned in EAX
    floats/doubles - returned in ST(0)

    This should be consistent across all compilers in order to be compatible with x86. If this system was not followed then it would be a nightmare to code low-level assembly since you would never know which register your quantity was returned in.

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Sorry, I was deviating from discussing the x86 architecture a little bit.

    Unless you know the system you are dealing with, you cannot be sure how return values will be returned. That is all I was getting at about ambiguity.

    For x86, yes, EAX is the main register to check for return values. This is the post I wrote on this subject that I was referring to. Obviously the programs I wrote there could well do with an update, but please keep in mind they are simply proof-of-concept.
    Last edited by MacGyver; 06-06-2007 at 12:51 PM.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by robatino View Post
    C++ does allow "void" as the argument type, it's just discouraged. (I'm not sure if it's deprecated or not.)
    It is simply redundant in C++.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Christopher2222 View Post
    Maybe ommitting the return 0; reduces program size and speeds up the execution of the program
    Are you serious?

    So why do people continue to use main() and main(void)? without int
    If you don't need argc and argv?

    If I started a program with main() and left out the return 0; it'll work okay right? and i won't run into any problems under the C99 environment?
    In C99, it's okay. But assuming that your environment is C99 probably isn't a good assumption just yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Bush vs. Kerry
    By jlou in forum A Brief History of Cprogramming.com
    Replies: 178
    Last Post: 11-29-2004, 03:45 PM
  3. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM
  4. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 08:58 PM
  5. Language
    By nvoigt in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-29-2002, 02:28 PM