> we do nothing with an integer there. so why say int?
The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored.
- Stroustrup http://www.stroustrup.com/bs_faq2.html#void-main
> Actually we can even omit int and the code still works
No, it doesn't.
Note also that neither ISO C++ nor C99 allows you to leave the type out of a declaration. That is, in contrast to C89 and ARM C++ ,"int" is not assumed where a type is missing in a declaration. Consequently:
See:
clang++: error: C++ requires a type specifier for all declarations
g++ (need to demand conformance with this compiler): error: ISO C++ forbids declaration of 'main' with no type http://coliru.stacked-crooked.com/a/142570a9f22f3f03
Microsoft: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int http://rextester.com/RXAUI71578
Edit: I didn't JLBorges post while I was doing mine :+)
Actually we can even omit int and the code still works.
Well, this is a pedantic warning, nevertheless one shouldn't promote bad habits.
1:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wpedantic]
The main function does return an int to the OS, it's just that the return 0; at the end can be omitted. One can return different values to indicate some kind of failure. This is handy when 2 programs must both run without errors before some other program or script can execute.
we do nothing with an integer there. so why say int?
The code that calls main is out of your control. The operating system's program loader has a line of code that is effectively int rc = main(ac, av); and you can't recompile it. It will execute that line of code regardless of whether your program defines the main function is int main(), void main(), or double main() (setting aside that neither 'void main' nor 'double main' are permitted in C++)
The program loader will read an int from wherever ints returned from functions go, always. On popular modern platforms, ints are returned in a CPU register, so the code doesn't crash, it just reads a junk value and makes the program poorly usable in batch files. I remember reading that there exist(ed?) platforms where returned values go on the stack and void main crashes.