Results 1 to 29 of 29

Thread: int main() or void main ()?

  1. #1
    Sadovod
    Guest

    Talking int main() or void main ()?

    What is really correct way ?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    int as return value is standard I think, I'll check it up
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The standard is:
    Code:
    int main(int argc, char **argv)
    (I can't remember whether envp is in the standard or not, but it's supported on quite a few compilers).

    You don't have to use all the arguments, so int main() is still valid.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    I always use int main, but my programming teacher insists on using void

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Silly fool
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Megatron
    Guest
    The ANSI standard suggests int, because void cannot return a value.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Both linux and windows want the app to return a value, so int main() is better, but not necessary, if you do void main() the return value wil usually be some random number if the CRT startup does not catch it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Are we talking about what you can get away with on x86, or what the standard says?

    On x86 the only difference return type makes is whether you get a real value or not, since it's returned by register. Some systems expect the value to be pushed onto the stack or something similar, and you can imagine the destruction caused if you didn't return a value
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yeah, but I haven't seen anyone on this forum ever talking about any other CPUs than x86 compatible.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It applies to me at least (the reason why I'm a Nazi about it comes out now ).

    At work I use MIPS and SPARC. Since I don't know much about their internals I have to follow things like this "just in case".
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Gee, what kind of weird job do you have?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Writing finite element analysis post-processing software that runs on Sun and SGI workstations

    Admittedly PCs (especially those dual Athlon monsters) are overtaking them now, but that's what we've got and when you've got 8 CPUs you don't talk back
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Cool! What does that mean? "finite element analysis post-processing". Is it me or just my lack of English?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Do a search for finite element analysis and you'll see what I mean
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    New Member
    Join Date
    Jan 2002
    Posts
    13
    Straut-soup said INT !

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's Stroustrup
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by setnewfocus
    Straut-soup
    LOL
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    whats the point of returning a value in main()?

    i use void 90% of the time, and if i wanna exit i just do exit(1)
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Because it tells the OS how your program did. Returning a value other than 0 indicates that your program exited abnormally.

    PS: I don't think exit is a good idea under C++, it might not call the destructors... *checks*
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  20. #20
    jim mcnamara
    Guest
    Look -

    Every language has baggage. C grew up under UNIX. Standard ansi C allows void main() as well as int main().

    Just like it allows goto.

    Does this make goto a good choice?

    NO!

    Under unix you return 0 for success (ERROR_SUCCESS) and any other number (almost always one) as failure.
    EXIT_FAILURE is a macro that is there for every version of c I've ever seen or heard of.

    Code:
    #define EXIT_FAILURE  1
    C images can be chained, therefore the calling image wants to know the status of the called image. _exec() chains images as does about 20 other fuinctions under VC++.

    Therefore, use int main() even tho I use void main() for C examples on this board a lot. This give you compatibility you may need later on.

  21. #21
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I thought it was the other way round, i.e. UNIX grew up under C.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  22. #22
    Zaei
    Guest
    There seem to be certain situations where goto is ok:
    Code:
    void x()
    {
       int* x = NULL;
       char* y = NULL;
       long* z = NULL;
       x = new int[120];
       y = new char[480];
       z = new long[120];
       ...
       if(failed)
       {
          delete [] x;
          delete [] y;
          delete [] z;
        }
        ...
        if(failed)
        {
          delete [] x;
          delete [] y;
          delete [] z;
        }
        ...
    }
    As opposed to:
    Code:
    void x()
    {
       int* x = NULL;
       char* y = NULL;
       long* z = NULL;
       x = new int[120];
       y = new char[480];
       z = new long[120];
       ...
       if(failed)
           goto err;
       ...
       if(failed)
          goto err;
       ...
       return;
    err:
       delete [] x;
       delete [] y;
       delete [] z;
    }
    This creates much cleaner code (no copy/pasting). Is this one of those few places where goto is ok?

    Z.

  23. #23
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    thats where i use it, especially if theres a bunch of cleaning up to do
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  24. #24
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's also where I thought goto had a rational use, but it's flawed, exceptions are much neater and prefered.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  25. #25
    Zaei
    Guest
    So, if failed, throw an exception?

    Z.

    [edit]
    I really cant type.

  26. #26
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    void x() {
        int* x = new int[120];
        char* y = new char[480];
        long* z = new long[120];
    
        try {
            // something fails
        } catch(std::exception &ex) {
            cerr << ex.what() << endl;
            delete[] x;
            delete[] y;
            delete[] z;
        }
    }
    Although that won't protect you against new failing, which technically is supposed to throw an exception of type bad_alloc (derived from the standard exception class). So the better solution here is to have the pointers wrapped into a managed class that will delete the data if others failed.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  27. #27
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    but parksie, why is that a better solution? Why not catch the bad_alloc specifically?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  28. #28
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Because you don't know which of the three new statements failed.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  29. #29
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That was indeed annoying problem that I didn't take into account, will consider a change in the exception handling rules in BORK.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width