Electronic – Who receives the value returned by main()


I know that in computers, value returned by the main() function is received by the operating system.
But, what happens in the main() function of a microcontroller?

Best Answer

On a microcontroller, main() is not really expected to ever exit, and the behavior if it does is not defined — so it's up to whoever wrote the C runtime for the microcontroller. I've seen systems that:

  • Have an implicit loop around main(), so that if it exits, it simply gets called again.
  • Have a simple "jump-to-self" loop (or a HALT instruction) that gets executed if main() ever exits.
  • Simply execute the rest of code memory that follows the call to main(). This is called "running off into the weeds".

I've never seen one that actually does anything at all with the value returned by main(). If this is something you actually care about, then you should take a look at — and possibly modify — the source code for your system's C runtime library.