Programming Fundamentals · Interview question

What does the return 0; at the end of main do?

A strong answer

It hands the integer 0 back to the operating system as the process's exit status, which is the one value your program reports to the outside world. By convention 0 means success and any non-zero value signals failure, so shells, make, and CI pipelines branch on it, which is exactly why cmd && next only runs next when cmd exited zero. You can inspect it directly with echo $? on Linux or macOS. Two refinements worth knowing. main is special-cased in C99 and later: falling off the end implicitly returns 0, which is legal but worth writing explicitly, and no other non-void function gets that treatment. And returning from main is not the same as returning from an ordinary function, the runtime then flushes stdio buffers and runs handlers registered with atexit before the process actually ends, which is why exit(0) and return 0 from main behave the same while _exit(0) skips that cleanup and can lose buffered output. On bare-metal embedded the whole convention evaporates: there's no OS to receive an exit code, so main is written never to return, and returning from it means falling into the startup code's trap loop or resetting the chip.

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

What Is a Program?

A program is text on disk until you compile it. Walk through the source → compile → run loop you'll repeat thousands of times.

More What Is a Program? questions

Browse all 472 interview questions
What does the return 0; at the end of main do? | EmbeddedPrep.io