16

In C89/C99/C11, in a freestanding environment, the entry point function is implementation-defined. In a hosted environment, it must be int main in a strictly-conforming program. Most modern compilers make void main an error. However, I see many users using void main. Even if it is allowed in a non-conforming compiler, why would one use it? I see no practical reason why void main would be preferred to int main. Even in C89, it's undefined behavior to leave off the return.

Is there a historical reason for the popularity of void main?

I don't believe my question is primarily opinion-based. Some valid ideas have already been presented in this thread, such as Microsoft's void main extension, and since Window's popularity, as well as it being the result of C books reprinting incorrect information. These are objective and historical reasons.

user4694281
  • 161
  • 3
  • 8
    Maybe because a lot of people don't care about returning a failure/success to the OS. – m0skit0 Mar 20 '15 at 14:04
  • 7
    Might have something to do with wanting to return 42 but nobody having any idea what it is supposed to mean. – Hans Passant Mar 20 '15 at 14:04
  • 1
    When I was starting out with C, I used `void main()` instead of `int main(void)` it because I thought it was "the proper way" to avoid writing `return 0` at the end of main... I was very lazy... ;) – Mints97 Mar 20 '15 at 14:05
  • 9
    People don't always know what they are doing. – juanchopanza Mar 20 '15 at 14:10
  • @juanchopanza But they learned it from somewhere. – user4694281 Mar 20 '15 at 14:13
  • From other people who didn't know what they were doing. It is turtles all the way down. – juanchopanza Mar 20 '15 at 14:15
  • 5
    See: [What should `main()` returnin C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c/18721336#18721336) for a comprehensive treatment of the subject. IMO, it's popular because (a) it allows you to avoid writing one line of code code and (b) Microsoft says it is OK on Windows machines. It also means that you don't care about the exit status of your program — you aren't writing code in a scripting environment where the exit status matters. – Jonathan Leffler Mar 20 '15 at 14:15
  • 4
    Ehh, Herb Schildt... http://www.lysator.liu.se/c/schildt.html – joop Mar 20 '15 at 14:18
  • Note that the C99 or C11 standard says: _The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: `int main(void) { /* ... */ }` or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): `int main(int argc, char *argv[]) { /* ... */ }` or equivalent;10) or in some other implementation-defined manner._ Microsoft's implementation defines `void main()` as another definition. – Jonathan Leffler Mar 20 '15 at 14:36
  • @JonathanLeffler Microsoft's implementation is C89 conforming. And C89 doesn't have "or in some other implementation-defined manner". – user4694281 Mar 20 '15 at 14:38
  • @user4694281: The C89 standard didn't specify "or in some other implementation-defined manner", but it was and is always possible for an implementation to define extensions to the standard (ask GCC!). And Microsoft's acceptance of `void main()` does not conform to the C89 standard, but is an implementation-defined way of defining `main()`. I don't like it at all, but that doesn't stop it being valid as an implementation-defined extension to the C89 standard when the code is created for Microsoft's C compiler. It isn't legitimate in any other hosted implementation that I know of. – Jonathan Leffler Mar 20 '15 at 14:53
  • @HansPassant ... /me looks through his projects ... most are GUI and simply return 0 ... commits a bunch of changes to main.cpp ... now all my `main`s return 42 ... it all means the same in the end :) – Kuba hasn't forgotten Monica Mar 20 '15 at 15:07
  • I don't believe this is primarily opinion based, it is a good question, just write good answers to it instead of guessing. Personally I would strongly suspect that one of the core reasons is ye ole Borland Turbo C, which became the industry de facto compiler/IDE even before C90 was released. – Lundin Mar 20 '15 at 15:11
  • @joop: Yes, I suspect Schildt carries a lot of the blame for `void main()`. He used it in his book "The Annotated ANSI C Standard", directly contradicting the standard he was annotating. – Keith Thompson Mar 20 '15 at 16:29

3 Answers3

11

Is there a historical reason for the popularity of void main?

The historical reason is, in my opinion, that books on programming in C (especially populer ones) were written before the Standard was adopted and were published after (or even before) the Standard were adopted. So many books on programming in C contained the declaration of main with return type void. Sometimes these books were republished without revising their contents. And due to the fact that old compilers usually supported the declaration of main with void such declaration were popular.

Also maybe some compiler producers (maybe even Microsoft. As you know C# allows to declare Main with void. At least Borland C++ allowed to use void main) introduced their own implementation-defined declarations of main. and main with void was a popular implementation defined declaration. So books on programming in C usually referred these popular compilers with their implementation defined main declaration.

nicoguaro
  • 2,869
  • 1
  • 25
  • 48
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
  • 1
    `void main()` wasn't really an option before the first C standard because `void` wasn't really part of C before the first C standard. What was semi-permissible was just `main()` — no explicit return type — but you were still supposed to return a value from such functions if the value was used. People didn't see the code that used the return value from `main()`, even though the shells got it. – Jonathan Leffler Mar 20 '15 at 14:18
  • 1
    IIRC (but my memory could be wrong, I am getting old) some old TurboC compiler on MSDOS accepted (or perhaps prefered) `void main()` this was in the early 1980s – Basile Starynkevitch Mar 20 '15 at 14:19
  • @Jonathan Leffler I have an old (relative to when it was written initially) book on MS C++ MFC (which suprisingly I bought not so far) there everywhere main was written like void main().:) – Vlad from Moscow Mar 20 '15 at 14:22
  • 1
    +1 to this answer since you've got the basic idea right, but I think most of these books were written post-standardization. The issue is just that their authors were incompetent but managed to get out books that lots of people read. IMO most of the responsibility for the misuse of the C language and people's misconceptions (generally negative views) about C can be traced to bad teaching in the form of bad books and bad instructors. – R.. GitHub STOP HELPING ICE Mar 20 '15 at 15:10
  • @R.. I think the second addition of my post is more relevant. MS and Borland compilers indeed have an implementation-defined declaration of main with void and books usually refered to these compilers because readers aslo have these compilers. – Vlad from Moscow Mar 20 '15 at 15:14
  • 4
    I suspect that Turbo C is the biggest culprit by far here, it was the completely compiler dominant back then, and it allowed void main(). Even today we still get confused students with crappy teachers posting questions with this form of main on SO. (Mainly because the backwards education system in a certain country which I won't name, seems to have nationally decided that Turbo C is still the cream of the crap and DOS is the OS of the future.) – Lundin Mar 20 '15 at 15:20
  • In principle, the very same standard that introduced the `void` keyword (ANSI C89) also introduced the requirement that `main` returns `int` for hosted implementations. In practice, some compilers may have introduced `void` before the standard was published. Apparently Turbo C is an example; I never used it myself. – Keith Thompson Mar 20 '15 at 16:25
  • 1
    "The cream of the crap" <3 – M.M Mar 28 '15 at 04:38
6

Quoting Lundin's answer,

If your program is running in a hostless environment (your program is an embedded system or an operative system), it may have any return type. void main() is most common.

If your program is running in a hosted environment (on top of an OS), main() must return int, and may have additional parameters.

EDIT:

As the OP is asking about a hosted environment, I may quote Keith's answer,

Similarly C has never permitted void main() other than as an extension; the same 1989 standard that introduced the void keyword defined the two standard definitions for main: int main(void) and int main(int argc, char *argv[]).

and Pochi's answer,

You generally want to know the exit status of your program. That's the reason why you have the int main() -- you return your exit status.

From these two convincing answers, I can conclude in my humble opinion that:

At the time of C89 / C99 / C11, C might not have permitted void main , but for small programs created by enthusiasts and learners and beginners, they might not be concerned about the return values at this stage, hence void main became popular.

Community
  • 1
  • 1
shauryachats
  • 8,610
  • 4
  • 32
  • 47
  • I don't see how this answers the question. – juanchopanza Mar 20 '15 at 14:10
  • @juanchopanza The OP asks *I see no practical reason why void main would be preferred to int main.* This is the reason. – shauryachats Mar 20 '15 at 14:11
  • @volerag I already mentioned the freestanding part in my question. I was asking about a hosted environment. – user4694281 Mar 20 '15 at 14:12
  • Being the person who wrote that answer at some point, I'm nowadays a bit less certain - the standard could actually be interpreted as "any implementation-defined way goes", even for hosted environments. But then you have to realize the meaning of implementation-defined, it means "it is ok for the compiler to do as it pleases in this case _as long as it documents how_". If no such compiler documentation exists for a certain non-standard form of main, then it should be treated as undefined behavior. – Lundin Mar 20 '15 at 15:15
  • In a *freestanding* environment, *you should not call a function `main` in the first place*. The `main` function is well-defined by the standard, and using a *different* type of `main` is just confusing. Call it `kmain` or `kernel_main` or something similar, and no-one will look askance at you for making its return type `void`. – DevSolar Mar 20 '15 at 15:21
3

If you are using *NIX or MAC do following

  1. Copy following code to test1.c

    void main() {
    }
    
  2. Compile code in command line

    cc test1.c
    
    gcc test.c
    
  3. Run following

    ./a.out
    
  4. Run following (this will show you what is returned by your program

    echo $?
    
  5. Save this code to test2.c

    void main() {
        int i = 5;
    }
    
  6. Repeat 2, 3 and 4 for test2.c

That is why it is not recommended.

Why was it popular? I do not know other answers than ones above. I do have a question though; was K&R 1st edition using void main() or int main()?

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • 4
    K&R 1st ed. couldn't possibly have used `void main` since `void` was not even invented at the time (it came to be not until C++ was born). K&R 1st ed. use implicit `int` in many places. – Jens Mar 21 '15 at 07:33
  • Thank you @Jens for answering –  Mar 21 '15 at 23:41