Thread: What's up with this?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    What's up with this?

    Take a look at this link please
    http://msdn.microsoft.com/library/de...m/basic_17.asp

    I'm having a "fight" with a couple of people from another forum because they insist on using void main and I keep saying that it is wrong. Now one of those people posted this link on the forum and I don't know how to explain why they used void main. My idea is because that page is really old and because MSDN is so big, no one had the time or energy to update old pages. Can someone help me win this argument? Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Who cares what Microsoft says? They're not the ANSI committee. Microsoft can do whatever they like. It doesn't make it correct. Just like when they "extended" Java and screwed it up. It wasn't anything SUN created, therefore, it wasn't standard. It was just some crap that they decided to pull. It's just like "fflush( stdin )" works in MSVC++. It doesn't mean it's correct. It's just Microsoft doing what they please. It's still wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I am not a language lawyer, but many compilers do not care if you declare main as void and will provide a zero return code for you automatically. I believe in C++ if you fail to supply an explicit return code, the return value is the last statement executed converted to an int. It is considered 'good form' to have main return an int with zero being OK and non-zero being some sort of error condition. A lot has to do with custom as well as the fact that C and C++ have been around for a long time and have had a lot of changes to them.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    It is illegal in C++.

    However, it is technically legal in C because of a loophole.

    http://homepages.tesco.net/~J.deBoyn...void-main.html

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it isn't. It's you misunderstanding. By the ANSI definition, main returns an int. Always. However, your implementation is allowed to let main return anything you want. That doesn't make your implementation ANSI compatable. Why? Because my implementation doesn't have to support whatever crap you allow.

    Just like you can add whatever extensions you want, and include them with your version of your compiler. It doesn't mean it's purely ANSI compatable any more. It's just your implementation.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Technically and pedantically, it is probably better to say it is not illegal (no, that is not a typo) for a compiler to support void main(). What the standard requires is that the compiler support the forms that return int, but it doesn't overtly disallow other forms (eg void).

    Code that uses void main() has no guarantee of compiling or executing correctly with a standard compliant compiler. Code that uses one of the defined int main() forms does have that guarantee.

    As to what Microsoft has done: it is also not illegal. AFAIK, all Microsoft C/C++ compilers also support int main() forms. Microsoft compilers (at least up to version 6) were famous for lack of standard conformance, but this is not one of the areas they have got wrong.

    If main() does not have a return statement (eg the code "falls off the end"), the effect is the same as if "return 0;" had been executed.

  7. #7
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    By the ANSI definition, main returns an int. Always. However, your implementation is allowed to let main return anything you want.
    I interpret it as : if the standard allows it, it's legal.

    It shall be defined

    * with a return type of int and
    o with no parameters [...] or
    o with two parameters [...] or equivalent;
    or
    * in some other implementation-defined manner.
    If it had said, "No, you can't leave it to the implementation; you MUST return int", then that's another story.
    Last edited by Dante Shamest; 06-21-2005 at 08:54 AM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I like C89's simpler version -- less wiggle room.
    "Program startup"

    The function called at program startup is named main. The implementation declares no prototype for this function. It can be defined with no parameters:
    Code:
    int main(void) { /*...*/ }
    or with two parameters (referred to here as argc and argv , though any names may be used, as they are local to the function in which they are declared):
    Code:
    int main(int argc, char *argv[]) { /*...*/ }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    .
    Join Date
    Nov 2003
    Posts
    307
    In all of the systems we use void main() has the potential of crashing scripts that call the code because void main() may return a random value. Standard scripting practice is:
    EXIT_SUCCESS means just that. We use sysexit.h values in both scripts and C code to give us a leg up on why something bombed. YMMV.

Popular pages Recent additions subscribe to a feed