Thread: int main vs void main

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 03:00 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.
    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 03:00 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 03:07 PM.
    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.

  5. #5
    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?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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!
    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
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  9. #9
    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.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    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.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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

  12. #12
    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++;
    }

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

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

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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

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, 06: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, 10:49 PM