Like Tree10Likes

int main vs void main

This is a discussion on int main vs void main within the C Programming forums, part of the General Programming Boards category; So, which year of the standard is that?...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    So, which year of the standard is that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,640
    Quote Originally Posted by Elysia View Post
    I am not aware of any standard in either C or C++ that allows the use of void main (though technically there was a possibility in C90).
    Many embedded compilers are often non-standard.
    Quote Originally Posted by C99 5.1.2.1 p1
    In a freestanding environment (in which C program execution may take place without any
    benefit of an operating system), the name and type of the function called at program
    startup are implementation-defined. Any library facilities available to a freestanding
    program, other than the minimal set required by clause 4, are implementation-defined.
    Emphasis mine. It doesn't even have to be called main, let alone return an int.

    EDIT: Guess Jim got there first.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    C99, huh? I stand corrected.
    And yet, they made an exception to make the standard utterly and completely useless for embedded systems that match that criteria. Well done.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    2,898
    From the C++ 0x draft standard.
    3.6
    Start and termination
    3.6.1
    1
    Main function
    [basic.start]
    [basic.start.main]
    A program shall contain a global function called main, which is the designated start of the program. It
    is implementation-defined whether a program in a freestanding environment is required to define a main
    function. [ Note: In a freestanding environment, start-up and termination is implementation-defined; start-
    up 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. — end note ]
    Jim

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Annonymous View Post
    Recently, I am seeing a lot of people use void main() instead of int main. Is this a good practice? I use int main, isn't that the correct way? Unless, your trying to shut off warnings about main not returning a value. What are the pros/cons? Which is better?
    It's not a choice. The correct forms of main are either...
    Code:
    int main (void)
      {
    
        return errorlevel;  // usually 0
    }
    or

    Code:
    int main (int argc, char *argv[])
      {
    
         return errorlevel;  //usually 0
    }
    Although it's apparently not taught in class... the operating system actually does make use of the return value of main() ... It's used in batch files and shell scripts, it's used when one program launches a child program and so on.

    If you use void main() and do not return a value, the address reserved for the return value is uninitialized, basicially containing a random number, and can cause wild misbehaviors in cases where a return value is expected.

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Annonymous View Post
    Ok, if this is not good practice. Why are people using it? Any ideas? Could the colleges possibly be teaching this practice?
    Yes, they are... Many schools, especially in India, are hopelessly out of date; still teaching with Turbo C (a 16 bit compiler who's output won't even ruin on 64 bit windows) and course designs that date back to MS-DOS.

  7. #22
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Quote Originally Posted by CommonTater View Post
    Yes, they are... Many schools, especially in India, are hopelessly out of date; still teaching with Turbo C (a 16 bit compiler who's output won't even ruin on 64 bit windows) and course designs that date back to MS-DOS.
    LOL - "ruin" - fantastic typo - and yet so apt.

    > It's used in batch files and shell scripts, it's used when one program launches a child program and so on.
    Yes indeed.
    Long ago, I found a fantastic little utility for a job I was doing that was rendered almost completely useless because the exit status was garbage.
    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.

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

Similar Threads

  1. [DEBATE]int main VS void main?
    By sudox in forum C++ Programming
    Replies: 20
    Last Post: 11-26-2010, 03:18 PM
  2. Why do people still use main() or main(void)?
    By Christopher2222 in forum C Programming
    Replies: 18
    Last Post: 06-06-2007, 07:34 PM
  3. A question about void(main) and int(main)
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 03:18 PM
  4. void main(), int main(), argc, argv[]????
    By Jonny M in forum C Programming
    Replies: 3
    Last Post: 03-06-2002, 09:12 AM
  5. why int main instead of void main?
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-26-2001, 11:49 PM

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