int main() vs void main() in esp-idf examples

commando_j
Posts: 27
Joined: Tue Feb 05, 2019 5:26 pm

int main() vs void main() in esp-idf examples

Postby commando_j » Tue Feb 05, 2019 5:37 pm

Hello,

I'm a C beginner (actually away for 15 years) and after a bunch of reading I think I have an answer, but would like a confirmation...

In the ESP-IDF example programs, like hello_world, the main functions start with void app_main(), instead of int main(). From what I gather, this is just a choice the Espressif developers made, and could have just as easily used int main(), and a return(0) at the end. Is this correct?
To be clear, I don't mean the difference between the wording "main" and app_main", but the choice to use "void" instead of "int".
If it is a choice, does anyone know why it was made? Google searches find many developers who do not like void main() at all, since they consider it "non-standard".
You're feedback is appreciated.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: int main() vs void main() in esp-idf examples

Postby ESP_Angus » Tue Feb 05, 2019 9:55 pm

Sure, I can explain this:

The C standard provides a standard "main routine" form, which is the "int main()" you're referring to.

Microcontroller programs usually also use "int main()" to conform to the standard, even though the main() routine on a microcontroller usually never returns. (Microcontrollers also don't have command line arguments, but you still sometimes see "int main(int argc, char* argv[])" in microcontroller C code, as well.)

(Note that main() is never the *very* first thing that runs in a standard C program - on any OS or on a microcontroller. Some internal startup code - usually provided by the toolchain - runs first and then main() is called.)

If writing standard C, having a "void main()" is a violation of the C standard and may emit a compiler warning. Probably in microcontrollers it doesn't matter at all, because the main() function won't ever return - or if it does return, something bad is going to happen regardless of whether it returns an integer value or not. On a "big" OS, "void main()" is a problem because a program should return an error code when it exits.

ESP-IDF's app_main() is not C standard "main()", which is why it's not called "main()" and doesn't return an integer.

The differences are:
  • app_main() is called inside a dedicated FreeRTOS task, after FreeRTOS scheduler is running.
  • app_main() can return and ESP-IDF will keep running. This is useful because, for example, you can start some other FreeRTOS tasks from app_main() and then return from app_main().
Some very technical details can be found here: https://docs.espressif.com/projects/esp ... on-startup

(Aside: there is some discussion thread here about the fact it can be irritating to developers that the ESP32 toolchain doesn't currently provide any way to have a "standard C" startup flow which calls a standard main() function. We plan to fix this limitation eventually, although this won't change anything bout how ESP-IDF apps are written.)

commando_j
Posts: 27
Joined: Tue Feb 05, 2019 5:26 pm

Re: int main() vs void main() in esp-idf examples

Postby commando_j » Thu Feb 07, 2019 11:33 pm

Thanks for the answer!

Who is online

Users browsing this forum: Baidu [Spider] and 117 guests