why int main instead of void main?

This is a discussion on why int main instead of void main? within the C Programming forums, part of the General Programming Boards category; Can someone explain to me why "void main" is wrong? Code like that can still be compiled (although it generates ...

  1. #1
    Unregistered
    Guest

    Question why int main instead of void main?

    Can someone explain to me why "void main" is wrong? Code like that can still be compiled (although it generates a warning) and run, and I have a textbook where every example has "void main" instead of "int main." Just curious. Thanks.

  2. #2
    Sayeh
    Guest

    why 'int main' not 'void main'

    Well, actually you have to think about this from the perspective of the operating system, not the perspective of you writing code.

    Regardless of how you prototype main(), the Operating System, arbitrarily expects the application to push an integer onto the stack at time of exit. You get away with 'void' here, because

    a) it doesn't matter, the whole application heap will be disposed of momentarily, and
    b) the application stub/wrapper that does the stack cleanup checks for an int to pull of the stack. If one isn't there, it returns a '0' (zero) to the O/S.

    Primarily, you always prototype with 'int main(...)' for

    a) good form, and
    b) compatibility (some compilers require this)

    enjoy.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    reply, what else :)

    ok dood, listen up.

    Basicly the main() was created to return somesort of value.
    So many programmer do int main(), and by the end of the main they put return(0); --> which is optional.
    Just to keep the compiler happy.

    Others do just main(), without any void or int before it, and no return(0); at the end of the main "function".

    Now, void main() will compile, but it's not correct, because it's not what the people that created this language planed, so... just do
    int main().

    Or if you're really lazy as i am, just do: main().

    Because let say you wrote void main() at the test, the teacher might take some points for it, but with MAIN() and INT MAIN() you can't miss!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    void main(){
    .
    .
    .
    }

    actually compiles exactly the same as

    int main(){
    .
    .
    .
    return 0;
    }

    You only MUST use int main() if you want to notify the operating system of an abnormal exit (i.e. an error).

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Actually, the ANSI standard says main returns an int so main should return an int irregardless of your OS/compiler.
    - lmov

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    http://www.eskimo.com/~scs/C-faq/q11.12.html

    void main(){
    // prog
    }
    is wrong, for the same reason that

    struct a { int a[10000]; } main( ) {
    struct a res;
    // prog
    return res;
    }
    is wrong - it's not an int.

    Oh sure, you just "get a warning" with both of them, but the latter one stands much more chance of blowing up in your face. It's only a matter of degree - is it a little wrong or a lot wrong?
    And how many little wrongs do you have to make before your program stops working, behaving oddly or whatever?

    > Others do just main(), without any void or int before it, and no return(0); at the end of the main "function".
    Whist this correctly declares main, it results in a garbage value being returned to the operating system.

    > Because let say you wrote void main() at the test, the teacher might take some points for it, but with MAIN() and INT MAIN() you can't miss!
    Unbelievably, I've heard of one such teacher who took marks away for using int main - sheesh!

    > Or if you're really lazy as i am, just do: main().
    The ability to implicitly declare functions as returning int (as this does) is likely to disappear in future.

    > I have a textbook where every example has "void main" instead of "int main."
    There are far too many books like this
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Sayeh
    Guest
    Salem, it's good to see you're still here. I'm kind of back from my little hiatus.

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Unbelievably, I've heard of one such teacher who took marks away for using int main - sheesh!
    Somebody should blow that mother ****s head off with an assult rifle.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or just make him write on the blackboard...

    "I will never teach 'void main( )' again. I will instead teach 'int main( )."

    ...about half a million times.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM

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