tags:

views:

2126

answers:

8

Does it matter which way I declare my C++ programs?

+26  A: 

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

And, Yes it does matter.

int main(int argc, char** argv)

or

int main()

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

void main(int argc, char** argv)

is not and was, IIRC, a pervesity that came with Microsofts C++ compiler.

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3

Alan
int main(void) is also proper
Trent
Actually, while that is okay, I think int main() is preferred to int main(void)
Alan
They are the *minimum* requirement for arguments (and it's "char *argv[]", not "char **argv"). Implementations are explicitly allowed to provide more forms. The int return type, however, is non-negotiable as you state.
paxdiablo
"char *argv[]" is the same as "char **argv"
DaMacc
+4  A: 

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.

Joe
Returning an int is also required for C89.
Dana the Sane
Actually, C89 is vague depending on how you read it. Interesting reading here: http://homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html
Joe
@Joe: that link not-withstanding, int is the required return type in C89 and C99 if you want your programs to be portable.
Simon Nickerson
@Simon: Agreed.
Joe
+5  A: 

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

Cannonade
It should return 0 as in return 0;
Kensai
A: 

If you're going by the spec, then you should always declare main as 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.

Electrons_Ahoy
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
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
Why is there value in doing the wrong thing (which happens to work in some cases), when the right thing is more work?
Tom
@Tom: because... the right thing was more work? I'm not sure that sentence parses correctly.
Electrons_Ahoy
@Ahoy - I missed one crucial word there... meant to say "...when the right thing is *no* more work"
Tom
@Tom - gotcha. And frankly, I agree with you.
Electrons_Ahoy
+2  A: 

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
A: 

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
A: 
ap
`gcc -pedantic`, however, will reject it. And not using `-pedantic` is just messed up.
Konrad Rudolph
Agreed, but I used gcc only as an example because it is perhaps the most popular compiler. The standard clearly allows a non-int return type for main() in C99, as per the text I highlighted in bold.
ap
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
+4  A: 

Bjarne Stroustrups made this quite clear:

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

http://www2.research.att.com/~bs/bs_faq2.html#void-main

vobject