摘自《C++ Primer Plus 6th edition》
The Function Header as an Interface
Right now the main point to remember is that C++ syntax requires you to begin the
definition of the main() function with this header: int main().This chapter discusses the
function header syntax in more detail later, in the section “Functions,” but for those who
can’t put their curiosity on hold, here’s a preview.
In general, a C++ function is activated, or called, by another function, and the function
header describes the interface between a function and the function that calls it.The part
preceding the function name is called the function return type; it describes information flow
from a function back to the function that calls it.The part within the parentheses following
the function name is called the argument list or parameter list; it describes information
flow from the calling function to the called function.This general description is a bit confusing
when you apply it to main() because you normally don’t call main() from other
parts of your program.Typically, however, main() is called by startup code that the compiler
adds to your program to mediate between the program and the operating system
(Unix,Windows 7, Linux, or whatever). In effect, the function header describes the interface
between main() and the operating system.
Consider the interface description for main(), beginning with the int part.A C++
function called by another function can return a value to the activating (calling) function.
That value is called a return value. In this case, main() can return an integer value, as indicated
by the keyword int. Next, note the empty parentheses. In general, a C++ function
can pass information to another function when it calls that function.The portion of the
function header enclosed in parentheses describes that information. In this case, the empty
parentheses mean that the main() function takes no information, or in the usual terminology,
main() takes no arguments. (To say that main() takes no arguments doesn’t mean
that main() is an unreasonable, authoritarian function. Instead, argument is the term computer
buffs use to refer to information passed from one function to another.)
In short, the following function header states that the main() function returns an integer
value to the function that calls it and that main() takes no information from the function
that calls it:
int main()
Many existing programs use the classic C function header instead:
main() // original C style
Under classic C, omitting the return type is the same as saying that the function is type
int. However, C++ has phased out that usage.
You can also use this variant:
int main(void) // very explicit style
Using the keyword void in the parentheses is an explicit way of saying that the function
takes no arguments. Under C++ (but not C), leaving the parentheses empty is the
same as using void in the parentheses. (In C, leaving the parentheses empty means you are
remaining silent about whether there are arguments.)
Some programmers use this header and omit the return statement:
void main()
This is logically consistent because a void return type means the function doesn’t
return a value. However, although this variant works on some systems, it’s not part of the
C++ Standard.Thus, on other systems it fails. So you should avoid this form and use the
C++ Standard form; it doesn’t require that much more effort to do it right.
Finally, the ISO C++ Standard makes a concession to those who complain about the
tiresome necessity of having to place a return statement at the end of main(). If the compiler
reaches the end of main() without encountering a return statement, the effect will
be the same as if you ended main() with this statement:
return 0;
This implicit return is provided only for main() and not for any other function
PS:
a,注意红色部分,int main()和int main(void)
C和c++是有区别的
C语言:int main(void)和int main()是有区别的
C++: int main(void)等于int main()
b,r版主说的是对的,标准C只有
int main(void) { /* ... */ }
和
int main(int argc, char *argv[]) { /* ... */ }
这两种写法