FAQ 2.02 What are the basics of main() ?

FAQ 2.02 What are the basics of main()?

graphics/new_icon.gif

It's the application's main routine.

Object-oriented C++ programs consist mostly of classes, but there's always at least one C-like function: main(). main() is called more or less at the beginning of the program's execution, and when main() ends, the runtime system shuts down the program. main() always returns an int, as shown below:

 int main() {   // ... } 

main() has a special feature: There's an implicit return 0; at the end. Thus if the flow of control simply falls off the end of main(), the value 0 is implicitly returned to the operating system. Most operating systems interpret a return value of 0 to mean "program completed successfully."

main() is the only function that has an implicit return 0; at the end. All other routines that return an int must have an explicit return statement that returns the appropriate int value.

Note that this example shows main() without any parameters. However, main() can optionally declare parameters so that it can access the command line arguments, just as in C.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net