[DEBATE]int main VS void main?

This is a discussion on [DEBATE]int main VS void main? within the C++ Programming forums, part of the General Programming Boards category; If you never intend to return, void main is just fine. Otherwise, you have no reason to use void main. ...

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    If you never intend to return, void main is just fine. Otherwise, you have no reason to use void main.
    Eh? Intent means nothing here. The standard requires int main. Who cares what you intend to do?

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by User Name:
    If you never intend to return, void main is just fine. Otherwise, you have no reason to use void main.
    The funny thing is that if you never intend to explicitly return from the global main function, int main is just fine. The use of void main, on the other hand, may result in a compile error.
    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

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    Quote Originally Posted by Memloop View Post
    The point is that you can use void main and still be compliant if you're using a freestanding implementation that supports it. You're not even required to have a main function to begin with if your compiler supports it. It's not going to be portable, but that's not always a problem.
    No question that, if your compiler supports void main(), you are allowed to use it.

    Also correct that a compiler which supports entry points other than int main() might be properly characterised as a freestanding implementation. There is also nothing in the standard preventing an implementation from being both freestanding and hosted - the difference between those two terms comes down to the minimum set of libraries that must be supported, not to the extensions supported.

    However, an implementation (compiler and library) that supports void main() is only compliant (or, actually, conformant) if it also supports int main().

    There is no actual definition in the standard of what compliant means for code (or for a programmer).

    If you mean that code which uses void main() is well-formed (the C++ standard defines a well-formed program to be "a C++ program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition Rule") then you are potentially correct, whether an implementation is freestanding or hosted.

    Although usage of void main() is likely to be diagnosed - by either the compiler or the linker - if the compiler does not specifically support void main().
    Last edited by grumpy; 11-26-2010 at 02:40 AM.
    Right 98% of the time, and don't care about the other 3%.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by grumpy
    However, an implementation (compiler and library) that supports void main() is only compliant (or, actually, conformant) if it also supports int main().
    That is an interesting observation. I assumed that "in a freestanding environment, start-up and termination is implementation-defined" meant that, like in C, "in a freestanding environment, the name and type of the function called at program startup are implementation-defined", but the statement that "all implementations shall allow both of the following definitions of main" clearly contradicts such an assumption.

    What does "start-up and termination is implementation-defined" really mean, then? All that is offered as elaboration is the statement that "startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration".
    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

  5. #20
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Unless there is an argument here on why to use "void main" I don't think there is a real debate.

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    Quote Originally Posted by laserlight View Post
    That is an interesting observation. I assumed that "in a freestanding environment, start-up and termination is implementation-defined" meant that, like in C, "in a freestanding environment, the name and type of the function called at program startup are implementation-defined", but the statement that "all implementations shall allow both of the following definitions of main" clearly contradicts such an assumption.
    Yep. My guess is that the standards committee had a philosophy of allowing as much freedom as possible, while having to specify (at least) a minimum set of requirements that an implementation must meet. Those types of goals imply some contradiction or trade-off.

    Quote Originally Posted by laserlight View Post
    What does "start-up and termination is implementation-defined" really mean, then? All that is offered as elaboration is the statement that "startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration".
    My interpretation is that startup is the process the implementation orchestrates before the first statement in main() (or any other entry point). That includes initialising standard I/O streams (stdout/cout, stdin/cin, etc), the process of ensuring constructors of objects with static storage duration are executed, and the process of preparing to call (and then calling) main().

    Similarly, I would interpret termination as the process that the implementation orchestrates after main() returns (capturing the return value from main() so it can be returned to the host environment, invoking destructors of objects with static storage duration, closing standard I/O streams, etc).
    Right 98% of the time, and don't care about the other 3%.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 03:14 AM
  5. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM

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