+ 2
Why does a code have to start with int main()?
They explain the importance of main(), but not having int right before it. What does it mean?
2 odpowiedzi
+ 5
main is the function that every C++ program will start from, always.
The "int" before it represents its return type, just like any other function. This is because main will always return an integer value, usually 0 if the program has run and ended successfuly, or another value such as 1, if the program encountered some problems while it was running, and it ended incorrectly. Having these return values helps to diagnose problems.
Always keep int as main, nothing else; it's the C++ standard, and some compilers won't even let you put a different return type.
+ 2
The return value of your program doesn't matter unless you launched it with another program. You can do things differently in the host program depending on that return value. But it is seldom used, so just put return 0 and forget about it.