The documentation is:
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts.
But when I run
int main() {
printf("%d", square(3));
return 0;
}
int square(int n) {
int sq = n * n;
return sq;
}
the program prints 9. So does the main() function get executed only after all the other functions or is it special in a different way?
The order is this:
main()
main()
calls square(3)
.square(3)
calculates the result 9
and returns it.main()
calls printf("%d", 9)
printf()
prints 9
on the terminal and returns the number of characters printed (1).main()
returns 0 to the OS.