Thread: Do we need rethinking?

  1. #1
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52

    Do we need rethinking?

    As said in the FAQ section,

    main() function should always return int.

    But this link makes me rethink:

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

    please consider this issue serious and express your views.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  2. #2
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    /me put's fireproof vest on and hides under desk.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > and a program with void main() is a conforming program.
    It's all hogwash. Conforming is to the standard, not a specific implementation of "funnysofts codemangler 7.2 compiler".

    Citing specific implementations does not constitute a standard - any compiler can implement main as returning whatever it wants. To be a "conforming" compiler (this is where that page really gets its knickers in a twist) is that it MUST accept int main as the return type, and issue a diagnostic if main isn't returning int.

    Notice that every compiler in (limited search of available compilers) uses the word "can" when referring to void main. This is far from being "only", and I'm sure that every compiler on the list also accepts int, and doesn't moan about it when you do.

    What is more, to be a conforming program, your code MUST be written with a main returning int. Sure you can write void main, double main, whatever main if your compiler accepts it in a non-conforming mode, but don't expect your code to be portable, and don't expect people to shut up about it.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    please consider this issue serious and express your views.
    I agree that the language used in the C Standard (ISO/IEC 9899:1999) allows void main() in a hosted environment. However, since the very words that allow it state that it is implementation defined, and implementations differ, it also means that void main() may, according to the C Standard, be non-portable.

    Use it at your own risk, just as you use any main() other than int main(void) and int main(int argc, char *argv[]) or their equivalents at your own risk.
    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
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    i fail to see how main's return type is such an issue, unless you
    are writing programs that execute other programs, or you
    want to signal something very specific to the compiler. I wont say
    this for certain, but i'm pretty confident that in compilers that allow
    void main, the compiler adds in a return 0 - just like when you
    forget to add it when main is an int. that's just my intuition on
    the subject and is in no way a fact. all the same, i
    dont understand how writing programs with int main and
    returning values is such a bg deal to so many programmers - its
    not hard to conform to the rules. the very fact that some
    compilers allow you to bend/break some rules is such that they
    are intoducing such confusion on the subject. just to point out,
    Microsoft Visual C++ 6.0 allows the following code:

    Code:
    struct something
    {
            /*yadayadayada*/
    };
    
    int main (void)
    {
             something anything;
             .
             .
             .
             return 0;
    }
    notice anything? i declared a struct of type something without
    a typedef and without the struct keyword. is this right? if it
    was, then why would nearly every programmer who uses a
    struct typedef it when defining it? Because its not legal!

    the way i see it, if people have such trouble with int return type
    of main in C/C++, let them learn C# which has no return type.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Having been the victim of a void main'er, that is wanting to use a program and hoping that some meaningful status was returned, I was thwarted by the ineptitude of that programmer. What actually got returned was the length of the last string printed - now that's useful

    I mean, if someone did
    int foo = sqrt ( 2 );
    and complained that it didn't work (prints 1 rather than 1.414.. ), everybody would be all over it like a cheap suit, telling them to fix the types (and rightly so).

    But somehow, the run time environment doing
    int result = main ( argc, argv );
    warrants a special kind of abuse just because the "actual assignment is out of sight, therefore it can't possibly matter to me" attitude.

    > i fail to see how main's return type is such an issue
    Neither can I. By learning to do it right when it doesn't matter means that you'll do the right thing out of habit when it does matter. Sure, if you want to "learn the dialect of C which my current compiler will let me get away with", that's fine by me, but don't go round saying it's OK for everyone else. Maybe you enjoy the pain and suffering which results when you move to another compiler and have to do the whole thing over.

    Personally, I go for the dialect of C defined by ANSI which doesn't give a rats ass which compiler you're using so long as that compiler claims to have an ANSI badge on it. As a result, the compiler of the day is a non-issue to me.

    > Microsoft Visual C++ 6.0 allows the following code:
    That's because what you posted is actually valid C++ code, but it is of course invalid C.
    Check which language you compiled it as.

    But it does illustrate the all too common problem of some people saying "but my compiler...." as some kind of proof of their argument.

    > but i'm pretty confident that in compilers that allow
    > void main, the compiler adds in a return 0
    It's my actual experience that such compilers return whatever junk was last in the register used for returning values from functions (see top of this post). New compilers allow int main to implicitly return 0, but if it's void, all bets are off.

  8. #8
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Thanks Salem, Richie T, laserlight . I think I may stick onto int main()
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  9. #9
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    Quote Originally Posted by Richie T
    . . .
    Code:
    struct something
    {
    /*yadayadayada*/
    };
     
    int main (void)
    {
    something anything;
    .
    .
    .
    return 0;
    }
    notice anything? i declared a struct of type something without
    a typedef and without the struct keyword. is this right? if it
    was, then why would nearly every programmer who uses a
    struct typedef it when defining it? Because its not legal!
    . . .
    The line:
    Code:
    something anything;
    is, in this case, C++ code not C (because the type something is not typedefed). A C-compiler (at least an ANSI strict C-compiler) will require:
    Code:
    struct something anything;
    So that is one reason why C programmers typedef their struct types so they don't have to write the keyword struct in front of the type.
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

  10. #10
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    and the award for pointing out some thing that was already
    mentioned 14 hours ago goes to...

    also, the reason behind my error, (since you just highlighted it
    again) was that my C++ lecturer told me that it wasnt standard.
    Perhaps he was mixed up between both languages or
    something. All the same i must get a copy of the standard for
    myself...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > was that my C++ lecturer told me that it wasnt standard.
    Pah, what do they know?
    Not much by most estimations, given the number of "my tutor said..." comments which are wrong.

  12. #12
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Salem
    > i fail to see how main's return type is such an issue
    Neither can I. By learning to do it right when it doesn't matter means that you'll do the right thing out of habit when it does matter. Sure, if you want to "learn the dialect of C which my current compiler will let me get away with", that's fine by me, but don't go round saying it's OK for everyone else.

    You've hit the nail on the head, Salem, with your "which my current compiler will let me get away with."

    All too often that's the case -- the attitidue is "how can I make these compiler errors go away", without understanding why they are there in the first place. It doesn't take a huge leap of faulty logic to then get to: "Well, if my compiler let me do void main(), then I would have one less error to deal with."

    A compiler is a tool. Imagine if someone decided to get around the restraints/requirements of a table saw in the same way they try to do with compilers. There'd be an awful lot of fingerless people out there

    And after all, how much effort does it REALLY take to put "return 0;" at the end of main() ?????

  13. #13
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    The C standard may (and in C89/C90, at least, did) allow for "void main()", and even "statusReturn_t main()", but (as far as I know) any conforming POSIX C environment, I believe, requires main() to return an int.

    As for using void main()? I suppose you *can*, but I suggest that you do not.

    As for me, I remember a time when you often saw:
    Code:
    main(argc, argv, envp)
    int argc;
    char **argv;
    char *envp;
    {
    }
    No type (void didn't even exist then), and three parameters.

    Those days are gone.

    I don't miss them.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed