In C, I know that int main()
returns an int
where void main()
does not. Other than that, is there a difference between them? Is the first better than the second?

- 28,141
- 6
- 41
- 93

- 649
- 2
- 6
- 7
-
10Use `int main(void)`, not `int main()` – Keith Thompson Feb 20 '12 at 05:49
-
4STOP READING HERE and go to the linked duplicate. All the answers below contain factual errors at some degree. Please refer to the linked duplicate. In particular, the answers posted by Jonathan Leffler and yours sincerely. – Lundin Dec 15 '17 at 13:06
3 Answers
The overwhelming majority of the time, one of int main(void)
or int main(int argc, char* argv[])
is what you need to use. In particular, if you're writing a program that's going to be compiled by any major compiler for running on a personal computer, with the full set of the C standard libraries, then you almost certainly need to be returning an int
from main
.
(I would also avoid using an empty argument list, see "Why don't we use (void) in main?")
The C99 standard does allow for other implementation-defined signatures, and you can use these if you've read the manual for your compiler and it says you can.
(5.1.2.2.1) It shall be defined with a return type of int and with no parameters ... or with two parameters ... or in some other implementation-defined manner
Personally I would avoid them even if they are allowed (if possible), because it's one more thing to worry about if you ever need to port to another system.
See the comments below "Why don't we use (void) in main?" for some interesting discussion on this.

- 29,332
- 18
- 93
- 152
-
15.1.2.2.1 applies only to hosted implementations. PICC32 presumably is freestanding, not hosted. All hosted implementations *must* permit `int main(void)` and `int main(int argc, char *argv[])` or equivalent. – Keith Thompson Feb 20 '12 at 05:48
-
1@KeithThompson - I've never been too clear on the difference between hosted and freestanding, but both the PIC32 compilers (optionally) use the full suite of C standard libraries, which I think means they can be both. – detly Feb 20 '12 at 06:03
-
-
@KeithThompson - From memory, I think the HITECH PICC32 did *not* (ie. it insisted on `void main(void)`) but that was always a bit of a weird compiler and broke the standard in other ways. Technically, sure, you can't call it a C compiler, but pragmatically, everyone did. – detly Feb 20 '12 at 08:47
-
1@KeithThompson If you quote the standard, quote the ENTIRE paragraph, not just the parts that aid your argument. That being said the rest of the line goes "... or equivalent; **or in some other implementation-defined manner.**" (C11 §5.1.2.2.1 - Program startup) Making it perfectly legal when the implementation allows for it. Detly is absolutely correct in his assessment that implementation-defined signatures are allowed. – Wiz May 12 '13 at 19:17
-
@Wiz: My point was that a hosted implementation *must* permit the two standard forma; a freestanding implementation needn't do so. For a hosted implementation, other forms like `void main(void)` are permitted *only* if the implementation documents them -- that's what *implementation-defined* means. Otherwise, the program's behavior is undefined. Many hosted compilers happen to accept `void main(void)` (sometimes with a warning); I don't know whether they actually *document* it. – Keith Thompson May 12 '13 at 20:05
-
1@Wiz: Apparently HITECH PICC32 actually *forbids* `int main(void)`; that means it can't be a conforming *hosted* implementation. It may or may not be a conforming *freestanding* implementation. You're absolutely correct that implementation-defined definitions of `main` are allowed; I never said otherwise. – Keith Thompson May 12 '13 at 20:08
-
1The comments here are mostly incorrect. A hosted implementation _does not_ have to support the two mentioned forms. The standard for _hosted systems_, which PIC is not, says black on white: "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)... **or** in some other implementation-defined manner. – Lundin Dec 15 '17 at 12:51
-
1PIC is a freestanding system so none of chapter 5.1.2.2 applies. Citing it to prove some point is simply incorrect. Correct chapter is 5.1.2.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." – Lundin Dec 15 '17 at 12:53
-
@Lundin - good catch, thanks! Five years ago I was not as familiar with the standard as I am now. – detly Dec 16 '17 at 11:31
If your book says void main()
it is very very out of date.
Unless you are in a very unlikely system where you have a freestanding main - see Why is the type of the main function in C and c++ left to the user to define?

- 2,272
- 2
- 17
- 32

- 94,801
- 28
- 188
- 263
-
5Please specify some reasons for the same as it is helpful for others including me – Moons Feb 20 '12 at 05:22
-
5
-
@kamal, void main() is wrong - except on a few embedded microprocessors where either void or int will work. – Martin Beckett Feb 20 '12 at 05:23
-
6@T.J.Crowder - I'm assuming the OP didn't invent void main() himself. There is a popular C reference book by an author I wont name (since his name summons up demons from hell) which used void main() among many other errors – Martin Beckett Feb 20 '12 at 05:25
-
1
-
I don't think that free-standing implementations are under discussion here. The question should only be about *hosted* implementations. C and C++ differ with regard to `main`, and C does allow *arbitrary* other declarations other than `int main()` and `int main(int, char**)`, while C++ requires than any `main` function return `int` (and both the `()` and `(int, char**)` signatures must be supported). – Kerrek SB Feb 20 '12 at 05:39
-
@KerrekSB: C allows *implementations to allow* other declarations. If the current implementation doesn't explicitly permit it, `void main(void)` causes your program's behavior to be undefined. – Keith Thompson Feb 20 '12 at 05:44
-
2`void main()` is not outdated; it's just wrong. It was not any more valid in earlier versions of the C standard. – R.. GitHub STOP HELPING ICE Feb 20 '12 at 05:48
-
-
@KeithThompson: Yes, quite. Implementations can accept other forms of `main`. But that means that there isn't a blanket ban on all non-standard mains, only that it is up to the implementation; but also this means that a non-standard form of `main` isn't automatically an ill-formed C program. By contrast, *any* C++ program with a non-`int` `main` is ill-formed. – Kerrek SB Feb 20 '12 at 06:19
-
@MartinBeckett: The `void` keyword was introduced by the 1989 ANSI C standard. – Keith Thompson Feb 20 '12 at 08:29
-
1You are all neglecting the obvious usage of void main() -- an embedded environment where the return type is of no consequence. Many embedded environments use C as a platform, and there is no point in returning anything on exit, as there is no kernel underneath to interpret that, and no other programs running to put it into context. – ArbitraryRenaissance Aug 19 '16 at 18:59
-
2Lots of incorrect comments here too. [Study this](https://stackoverflow.com/a/31263079/584518). Implementation-defined forms of main() has _always_ been allowed in C _and_ C++ for freestanding systems (some confused people like Bjarne Stroustrup has falsely claimed otherwise, because they weren't aware of the existence of the freestanding chapter in the C and C++ standards respectively). As per C99 and beyond, implementation-defined forms are also allowed for hosted systems. – Lundin Dec 15 '17 at 13:00
-
@Lundin How did Bjarne Stroustrup claim otherwise? A citation or link(s) to more information would be appreciated. – Aykhan Hagverdili Mar 28 '20 at 10:55
-
@Ayxan It's in his FAQ [here](http://www.stroustrup.com/bs_faq2.html#void-main), he quote the hosted implementation sub-chapter. Even if he is technically speaking of a strictly conforming program (that is, a program which doesn't contain any implementation-defined behavior at all), then in that case it is equally correct to say that C++ has never been useful for embedded systems or hardware-related programming. Because it isn't possible to have a freestanding system without impl.defined behavior. – Lundin Mar 30 '20 at 06:26
void main()
is not valid C. int main()
is. That's the main difference.

- 208,859
- 35
- 376
- 711
-
-
5
-
3
-
2No, it is never ok to write `void main()` unless you're using a *freestanding implementation*, in which case it may be valid (implementation-defined). A freestanding implementation of C is one that does not provide any of the higher level C standard library features; in practice it's going to be either a super-minimal embedded system or the foundation for an operating system kernel. – R.. GitHub STOP HELPING ICE Feb 20 '12 at 05:46
-
9@R.. I just randomly came across this question; your answer is absolutely wrong. This has nothing to do with freestanding implementation. C11 §5.1.2.2.1 - Program startup - clearly says "int main(void)", "int main(int argc, char *argv[])" or equivalent; **or in some other implementation-defined manner**. Therefore it's 100% valid C if the implementation allows for it. The only thing it's not is a `strictly conforming program` according to the standard. – Wiz May 12 '13 at 19:09
-
1@Wiz: If the implementation *doesn't* allow for it, then the behavior of a program that uses `void main()` is undefined. `void main()` (or `void main(void)` is *conditionally* valid, but there is no benefit in using it under a hosted implementation (particularly since, as of C99, falling off the end of `main` does an implicit `return 0;`). – Keith Thompson Nov 20 '13 at 00:04
-
1Freestanding systems may support the full C standard with all libraries. At a minimum, they must support some standard headers as stated in C11 4/6. – Lundin Dec 15 '17 at 13:04