Advertisement

c++

Started by February 06, 2003 08:22 PM
19 comments, last by Fingolfin 21 years, 9 months ago
Depends what you want to do.
If your main does not return, then

void main()
{
} is good.

I prefer
int main()
{
return 0;
}
because I want 0 return.

int main()
{
}

unfortunately will not compile, because you arent returning anything.
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
quote: Original post by Anonymous Poster

But we might aswell help him out with standard C++ if we're going to give him examples.

Main does NOT take void as an argument and it doesn't HAVE to return anything.

Cheers.


Wrong! The C standart says that main _must_ return an int.
Nevertheless most compilers will accept "void main"

[edited by - noVum on February 7, 2003 7:06:37 AM]
Advertisement
Not only does the standard say that main must return an int but it also says that falling off the end of main implies a return of 0. So,

int main()
{
}

is the exact same thing as
int main()
{
return 0;
}

It is simply a matter of style whether you want to type the return statement or not.
Why does a thread about helping someone start C++ end in a discussion whether main() is required to return an int?

I don''t have a signature
I don't have a signature
MSVC just doesn''t handle this correctly, so for MSVC, either "return 0" manually or use the void declaration (which goes against the standard but works nonetheless)

Kippesoep
Straight from the standard:

quote: An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.


So the arguments to main are arbitrary, although the standard requires that both

int main();
int main(int argc, char * argv[]);

be allowed. The standard also states:

quote: A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function.


Which by my understaning allows for nonstandard entry points like WinMain.
Advertisement
quote: Original post by Brian Jones
Why does a thread about helping someone start C++ end in a discussion whether main() is required to return an int?
The communist GameDev has always been like this

return 0;
quote:
int main(){}

Build: 1 succeeded, 0 failed, 0 skipped
Compile Success
MSVS.net
The C++ standard requires that compilers allow the main function to be declared as int, and requires that they allow a main function with a void or int,char** argument list. However, all compilers I know of allow (with warnings) a main function to return void, or to fall off the end of the int main function without returning a value. In such cases, 0 is returned.

And that''s all I have to say about that.

Don''t listen to me. I''ve had too much coffee.
The college ref book for C++ from Deitel and Deitel is called C++ How to Program. I have the C version and it is an excellent book to learn how to program. I love it if I could ever find it again...it got misplaced when I moved. Great book for reference and/or to teach you the language.
Until Then, I Remain,Brandon

This topic is closed to new replies.

Advertisement