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; Recently, I am seeing a lot of people use void main() instead of int main. Is this a good practice? ...

  1. #1
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302

    int main() vs void main()

    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?
    Last edited by Annonymous; 10-20-2011 at 04:00 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    I use int main, isn't that the correct way? Unless, your trying to shut off warnings about main not returning a value.
    I noticed my hand was bleeding. So I chopped my arm off. Hand stopped bleeding, so I know I fixed the problem.
    timmmay, Elysia, Salem and 1 others like this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    How the fvck does that answer my question?
    Last edited by Annonymous; 10-20-2011 at 04:00 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,879
    Is this a good practice?
    No!
    I use int main, isn't that the correct way?
    Absolutely.
    What are the pros/cons?
    One is non-standard (void). The other is standard.
    Which is better?
    Follow the standard.

    Jim
    Salem and Annonymous like this.

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Operating systems will sometimes make use of the return value when your program ends and you return to the OS. If you don't return anything, the "return value" will be garbage and the results will be unexpected and mysterious.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    Quote Originally Posted by Annonymous View Post
    How the fvck does that answer my question?
    It means, you could either bandage your finger (add a return statement), or chop off your hand (use void main).
    brewbuck simply related chopping off the hand to void main because it's non-standard. And why use a non-standard construct when there's a perfectly legitimate and easy way to fix it?

    Quote Originally Posted by TheBigH View Post
    Operating systems will sometimes make use of the return value when your program ends and you return to the OS. If you don't return anything, the "return value" will be garbage and the results will be unexpected and mysterious.
    Not quite true. If you use void main, you will be in the land of undefined behavior. Some compilers actually make sure to return 0 if you use void main (eg Visual C++) and some will just return whatever happens to be present in the register used for returning variables (typically eax; eg, GCC).

    Though you are correct in that it will have undefined behavior.
    Last edited by Elysia; 10-20-2011 at 04:07 PM.
    TheBigH likes this.
    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.

  7. #7
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Ok, if this is not good practice. Why are people using it? Any ideas? Could the colleges possibly be teaching this practice?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    Since this is such a common issue, we actually have a great link explaining all about it: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]). At the end of that article, it links to a great page. I'll repost the link here, just so you don't miss it: void main(void) - the Wrong Thing.
    Annonymous likes this.

  9. #9
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    Thank you Anduril462

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    Quote Originally Posted by Annonymous View Post
    Ok, if this is not good practice. Why are people using it
    Cluelessness is one factor.
    Another is dumb colleges teaching it.
    Yet another is dumb books teaching it.
    And yes, even tutorials.
    Even MSDN, Microsoft's great developer network still have articles that uses void main.
    And, in fact, I've even seen some Microsoft devs use it in their presentations on the recent build conference.
    Lastly, there are some non-standard conforming devices (particularly embedded devices) that simply use void main (usually because int main makes no sense).

    Could the colleges possibly be teaching this practice?
    Yes!
    Annonymous likes this.
    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.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,879
    Ok, if this is not good practice. Why are people using it? Any ideas? Could the colleges possibly be teaching this practice?
    Yes many institutions of "higher learning" teach this bad programing practice, using outdated, pre-standard compilers. Many of the universities in India are a prime example.

    Jim
    Annonymous likes this.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    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?
    Some implementations allowed it before there was a standard. Some implementations (e.g. Turbo C) still allowed it even after the standard. When you see it on this board, it's often from a Turbo C user. Many/most/all of India's universities still teach on the very outdated Turbo C, and many of them come here for help, hence all the instances of void main.

    Note, in unhosted environments like embedded systems (one's without a "typical" OS), the standard does not require int main, since there is no environment to return a value to. Thus, an embedded compiler may allow or even require void main, though personally I have not seen such an implementation. We occasionally see void main used on embedded projects here, though it's much less common than the cancerous Turbo C.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    Quote Originally Posted by anduril462 View Post
    Note, in unhosted environments like embedded systems (one's without a "typical" OS), the standard does not require int main, since there is no environment to return a value to. Thus, an embedded compiler may allow or even require void main, though personally I have not seen such an implementation. We occasionally see void main used on embedded projects here, though it's much less common than the cancerous Turbo C.
    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 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.

  14. #14
    Registered User Annonymous's Avatar
    Join Date
    Apr 2011
    Location
    Jackson, New Jersey, United States
    Posts
    302
    anduril462

    Since this is such a common issue, we actually have a great link explaining all about it: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]). At the end of that article, it links to a great page. I'll repost the link here, just so you don't miss it: void main(void) - the Wrong Thing.
    The return type of main() must always be an int, this allows a return code to be passed to the invoker.

    Thanks everyone! Your answers and feedback are much appreciated!
    Last edited by Annonymous; 10-20-2011 at 04:27 PM.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,879
    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).
    From the draft INTERNATIONAL STANDARD ŠISO/IEC ISO/IEC 9899:201x

    5.1.2.1 Freestanding environment
    1
    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.
    2
    The effect of program termination in a freestanding environment is implementation-
    defined.
    Jim

Page 1 of 2 12 LastLast
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