57

Does it matter which way I declare the main function in a C++ (or C) program?

Pedro Boechat
  • 2,049
  • 1
  • 18
  • 24
Kredns
  • 34,183
  • 49
  • 147
  • 200

8 Answers8

76

The difference is one is the correct way to define main, and the other is not.

And yes, it does matter. Either

int main(int argc, char** argv)

or

int main()

are the proper definition of your main per the C++ spec.

void main(int argc, char** argv)

is not and was, IIRC, a perversity that came with older Microsoft's C++ compilers.

https://isocpp.org/wiki/faq/newbie#main-returns-int

Alan
  • 42,055
  • 16
  • 107
  • 130
28

Bjarne Stroustrup made this quite clear:

The definition void main() is not and never has been C++, nor has it even been C.

See reference.

Azeem
  • 7,094
  • 4
  • 19
  • 32
vobject
  • 5,050
  • 3
  • 26
  • 21
13

You should use int main. Both the C and C++ standards specify that main should return a value.

Azeem
  • 7,094
  • 4
  • 19
  • 32
RedBlueThing
  • 39,658
  • 17
  • 95
  • 121
9

For C++, only int is allowed. For C, C99 says only int is allowed. The prior standard allowed for a void return.

In short, always int.

Azeem
  • 7,094
  • 4
  • 19
  • 32
Joe
  • 38,368
  • 16
  • 103
  • 119
5

The point is, C programs (and C++ the same) always (should?) return a success value or error code, so they should be declared that way.

Svante
  • 46,788
  • 11
  • 77
  • 118
4

A long time ago I found this page (void main(void)) which contained many reasons outside of the "the standard says it is not valid" argument. On particular operating systems/architectures it could cause the stack to become corrupted and or other nasty things to happen.

X-Istence
  • 15,338
  • 6
  • 52
  • 73
3

If you're going by the spec, then you should always declare main returning an int.

In reality, though, most compilers will let you get away with either one, so the real difference is if you want / need to return a value to the shell.

Azeem
  • 7,094
  • 4
  • 19
  • 32
Electrons_Ahoy
  • 30,983
  • 34
  • 101
  • 126
  • 1
    You should always try to do things "by the spec" unless there is a compelling reason not to. Granted, a lot of implementations allow void main, and other extensions. This does not mean you should rely on implementation specific extensions. In embedded applications, with no shell, void main is ok. – Trent Mar 12 '09 at 00:09
  • I'm going to go ahead and agree with all of that. For a while, I got into the habit of using void vs int as a kind of pesudo-comment about whether I was expecting to return a value, but now I just always use int. – Electrons_Ahoy Mar 12 '09 at 00:17
  • Why is there value in doing the wrong thing (which happens to work in some cases), when the right thing is more work? – Tom Mar 12 '09 at 03:31
  • @Tom: because... the right thing was more work? I'm not sure that sentence parses correctly. – Electrons_Ahoy Mar 12 '09 at 05:52
  • @Ahoy - I missed one crucial word there... meant to say "...when the right thing is *no* more work" – Tom Mar 12 '09 at 12:30
  • @Tom - gotcha. And frankly, I agree with you. – Electrons_Ahoy Mar 12 '09 at 20:50
  • +1 for being concise. I dunno how I got here, I didn't need to know, but found myself reading the other answers. It really just takes a sentence or two. – dyasta Mar 14 '17 at 00:35
  • @Trent The spec is a moving target. When I first used C++ (it was around 1992), void main(void) was the norm. And it makes sense it exists, given there is also bare metal embedded software where the end of main is end of "intelligent" device ;) – BitTickler Jan 19 '20 at 03:11
2

In C++, main() must return int. However, C99 allows main() to have a non-int return type. Here is the excerpt from the C99 standard.

5.1.2.2.1 Program startup

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; or in some other implementation-defined manner.

Also note that gcc does compile void main() although practically, it does a return 0; on encountering a closing brace.

Masked Man
  • 1
  • 6
  • 35
  • 77
  • 7
    `gcc -pedantic`, however, will reject it. And not using `-pedantic` is just messed up. – Konrad Rudolph Jan 17 '10 at 12:54
  • 1
    If you rely on implementation-defined behavior, then your program is not standards-conforming. Your compiler may accept it, but it's not valid C99, just foo-C99. – Jed Aug 27 '10 at 15:41
  • @Konrad Rudolph Hey, if we are not using ```gcc -pedantic```, does that mean we are not using the standard C? Secondly, I would appreciate it if you tell me where it is written. – Niraj Raut Jan 22 '21 at 15:42
  • @Jed Hey, if we are not using ```gcc -pedantic```, does that mean we are not using the standard C? Secondly, I would appreciate it if you tell me where it is written. – Niraj Raut Jan 22 '21 at 15:43
  • @NirajRaut No, on the contrary. I *always* use at least `-pedantic`, or `-pedantic-errors`. There’s absolutely no reason to use an incorrect return type, it’s just sloppy (but note that free-standing C implementations have different entry point functions!). – Konrad Rudolph Jan 22 '21 at 16:18