The C standard allows two forms of environments: freestanding and hosted.
- Freestanding systems refers to systems without a file managing OS, such as microcontroller applications, RTOS applications, applications that are the OS.
- Hosted systems refer to programs running on top of an OS: Linux, Windows etc.
Hosted systems, and only hosted systems, have a special requirement that they must support the standard form
int main (void). So when we have the flood of PC programmers entering the branch of embedded systems programming, they bring religious dogmas about
main with them, because they only know the hosted system requirements.
But since almost all embedded systems are freestanding systems, we can completely ignore hosted systems here on the E.E site. This is what the standard says:
ISO 9899:2018 18.104.22.168 Freestanding environment
In a freestanding environment (in which C program execution may take place without any benefit
of an operating system), the name and type of the function called at program startup are implementation-defined.
Implementation-defined means that the compiler decides which forms that are allowed. The most common form is
void main (void). Some systems don't even have
main, others may name it differently. What's important to realize is that the compiler makes this decision, never the programmer.
The problem with using
int main (void), if supported by the compiler, is that it almost certainly generates calling convention overhead. The reset vector and CRT will call main and push data onto the stack, that just sits there as dead space. Various dirty hacks (like manually resetting SP) can dodge this waste, but generally
int main (void) is bad form in freestanding systems, since it is wasteful and senseless. What are you going to return to?
If there is a
return 0 or not in the end doesn't matter. In standard C,
int main(void) is a special function and if missing a return statement, it is equivalent to writing
return 0. Only the
int main (void) function has this special rule, not ordinary functions.
So what you should do is to check which forms of
main() your compiler allows, then pick one without a return statement. For example, gcc supports the preferred form
void main (void) if you compile with
For more information:
I wrote a detailed answer on Stack Overflow, addressing both freestanding and hosted systems, in all versions of the C and C++ standards (at the point when it was written): What should main() return in C and C++?