-1

I was taught that the return at the end of main function is a signature to the program, which indicates wether or not the program completed sucessfully.

It seems like this idea does not hold for in micro-controllers, because you write a program in a while(1) loop , not even operating system like free RTOS.

Why, in such a case, would you return 0 at the end of main? Who or what recieves the return?

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • I don't understand your question, because when violets are red, not even I can follow. – Karoly Horvath Jul 05 '16 at 13:39
  • 1
    In C `void main(void)` is valid, it's unspecified what happens when the function returns, but it is valid. And if you're writing the main function of an operating system, and the only way to exit that main function is to power off the system or programatically restart it, then who cares what you return, because it's not going to return anyway. ***On the other hand***, if you write a "normal" process that is supposed to have a beginning *and* n end, then you should probably be following the normal "standard" and return `0` for okay, and a small positive value for "failure". – Some programmer dude Jul 05 '16 at 13:41
  • 2
    I don't understand and I don't use return in the main function of my microcontrollers. There's an infinite loop, I stay in it. If an error that I can't manage happens I let the watchdog restart the microcontroller, but I don't return from main. – Tim Jul 05 '16 at 13:41
  • 2
    If the return value is not used in a stand-alone environment, a good compiler will optimize out the `return 0;`. As `int main()` can be recursively called, return an `int`, either explicitly or implicitly is good coding. – chux - Reinstate Monica Jul 05 '16 at 13:44
  • 1
    See the linked duplicate. Several good answers there, some of them address "freestanding systems". Also see [this](http://electronics.stackexchange.com/questions/55767/who-receives-the-value-returned-by-main) question over at E.E, the answer by yours faithfully should answer your question as well. – Lundin Jul 05 '16 at 13:49
  • @JoachimPileborg: `void main` is only valid for freestanding environments. – too honest for this site Jul 05 '16 at 13:50
  • @Olaf Even hosted systems allow implementation-defined forms of main, since C99. The standard isn't really clear. See the linked duplicate, particularly [this](http://stackoverflow.com/a/31263079/584518) answer. – Lundin Jul 05 '16 at 13:51
  • @Lundin: The exact text is really not good phrased, as it is not clear what the "or in some other implementation defined..." part binds to: the whole or just the parameters (I do prefer the parameters ony, but there should be a defect/unclear report for the paragraph). But the environment can add additional restrictions. Do you know any hosted environment which does **not** require main to return an `int` result? – too honest for this site Jul 05 '16 at 13:57
  • @Olaf MS DOS! :) And probably various old versions of Solaris. Also some hosted systems, most notably [Windows](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633559(v=vs.85).aspx), use completely implementation-defined forms of main. – Lundin Jul 05 '16 at 14:05
  • @Lundin: I just asked about the result type, not the arguments (the arguments are not relevant here). And for the arguments: IIRC they still allow for `int, char **`, but add more arguments. Btw. POSIX does the same. – too honest for this site Jul 05 '16 at 14:11

1 Answers1

2

On a hosted environment, The signature of main allows for two basic variants:

int main(void)

int main(int, char **)   // or synonym like char *[]

Strictly speaking the return 0 is never required for main, as the standard clearly states that 5.1.2.2.3:

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

By long time established convention 0 means "no error" on such systems (that's the reason for the default result).


Said that, a bare metal controller is typically a freestanding environment. For such environments, the signature of main is not specified. Actually not even which function is called as entry to the application program is not defined, but left to the environment. Using main is just a common informative convention here. It often has a signature of

void main(void)

Even better, they define

_Noreturn void main(void)

That means main shall never ever return.

However, some systems allow main to return and flag for special actions, e.g. cold reset, hot reset, signal fatal failure (like busy-blink an LED), power off, etc. So you require one of the classic signatures here.

Note all this is beyond the standard and has to be defined by your execution environment.


Briefly: return 0 from int main(...) is not required for a hosted environment, but it is good style to be explicitly state: "Yes, there is no error here". For a freestanding environment like your's Check the documentation and/or startup code for the correct signature and the allowed results.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 2
    Strictly speaking a `return some_value;` is required if code performs a call to `main()` in either a hosted or non-hosted environment (and no preceding `exit` nor infinite loop). OTOH, I'm not a fan of code calling its own `main()`. – chux - Reinstate Monica Jul 05 '16 at 15:22
  • @chux: No! for a freestanding environment, the standard does not have **any** requirement on how the application is started (I think I wrote that). Neither for the name, nor the parameters or result. IOW, there is no special (no declaration allowed, automatic `return 0` ) `main` in freestanding code. Just a normal function which **may** be called `main`, but need not (and you have to declare it). See 5.1.2.1p1. And there is also no requirement for `exit` or `abort`, just few mandatory headers which don't contain any code. – too honest for this site Jul 05 '16 at 16:08
  • @chux: But I agree, any application entry-function(s) should not be called recursively. – too honest for this site Jul 05 '16 at 16:14
  • 2
    5.1.2.2.3: refers to an "a return from the initial call". A subsequent call (recursion) of `main()` is not the initial call and does not enjoy the missing `exit/return 0;` option specified in 5.1.2.2.3. So if code calls `main()`, then `main()` needs an explicit return value. – chux - Reinstate Monica Jul 05 '16 at 17:52