Embedded Systems Fundamentals · Interview question

Why must main() never return on a bare-metal system?

A strong answer

Because there's nowhere to return to. On a hosted system, main returns to the C runtime, which returns to the OS, which reclaims the process. On bare metal there is no OS and no caller beyond the startup code, the reset handler calls main as essentially the last thing it does. If main returns (or falls off the end), control goes back to the startup code, which on a correctly written runtime traps in an infinite while(1) to contain it; but the device is now doing nothing useful, peripherals may be left in arbitrary states, and if the trap weren't there, the PC could run off into whatever follows in memory and execute garbage, likely faulting. So bare-metal main is designed as a non-terminating super-loop (while(1) { ... }) or it hands control to an RTOS scheduler that never returns. Treating main as a function that returns is a hosted-programming habit that doesn't fit the bare-metal model.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Boot Sequence & Startup Code

Everything that runs before main(): the reset vector, copying .data and zeroing .bss, the vector table, and how a bootloader hands off via VTOR.

More Boot Sequence & Startup Code questions

Browse all 472 interview questions
Why must main() never return on a bare-metal system? | EmbeddedPrep.io