The main function
Why is the only function in the previous example called main? The simple answer is that the code
won’t compile if it isn’t! However, it might be more useful to explain how the language works.
A normal C++ application contains many functions (and also many classes, as is discussed in
Chapter 2, “Introducing object-oriented programming”). How does the compiler know which function
should be called first? Obviously, you can’t allow the compiler to just randomly choose a function. The
rule is that the compiler always generates code that looks for a function named main. If you omit the
main function, the compiler reports an error and doesn’t create a finished executable application.
Do'stlaringiz bilan baham: |