🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →The main Function Explained
Lecture 19 of our free C programming course: how every C program starts, what main returns, and how main behaves differently in embedded C programming.
Every C program you will ever write begins at one special function, and this lecture of our free C programming course explains it fully. If you are learning embedded C programming, you will discover that main behaves a little differently on a microcontroller than on a laptop, and knowing both stories makes you a stronger firmware developer. This free C tutorial covers the signature, the return value, exit codes, command line arguments and the startup code that runs before main on real hardware, so it also supports anyone searching for a free embedded C course that goes beyond the basics.
What You Will Learn
- Why execution always begins at main
- Valid signatures of main in standard C
- The meaning of the return value and exit status
- How return from main differs from calling exit
- How command line arguments reach main
- What happens before main on a microcontroller and why main never returns there
Prerequisites
You should know how to define and call functions, return values from them, and read a simple program. All lectures of the Functions chapter up to this point are helpful.
Why Execution Starts at main
A C source file can contain dozens of functions, yet the program needs one agreed starting point. The language designates the function named main for that job. The rule is simple: in a hosted environment such as Linux, Windows or macOS, the runtime support code calls main, and when main finishes, the program ends. Every other function runs only because main calls it, either directly or through a chain of calls.
Only One main
A program has exactly one main. Defining a second one produces a linker error about multiple definitions, because the name must be unique across the whole program, not just a single file.
Name Fixed, Signature Fixed
The main function is written by you, but its name and the types of its parameters are predefined by the language. You cannot rename it, and you cannot give it arbitrary parameters. Standard C accepts two forms:
int main(void)
{
/* program body */
return 0;
}
int main(int argc, char *argv[])
{
/* program body using command line arguments */
return 0;
}
| Form | Use |
|---|---|
| int main(void) | Program takes no command line input |
| int main(int argc, char *argv[]) | Program reads command line arguments |
| void main(void) | Not standard on hosted systems, avoid it |
| main() | Old style, implicit int, obsolete |
Older books write plain main() without a type. That relies on the obsolete implicit int rule from earlier lectures. Always write int main(void) in new code.
The Return Value of main
On a hosted system main returns an int to whoever started the program, usually the operating system or a shell script. The convention is universal: zero means success and any nonzero value means some kind of failure. Scripts and build tools use this status to decide what to do next.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int sensors_found = 0;
if (sensors_found == 0) {
printf("no sensor detected\n");
return EXIT_FAILURE;
}
printf("sensor ready\n");
return EXIT_SUCCESS;
}
gcc -o check_sensor cp6_19_check.c
./check_sensor
echo $?
no sensor detected
1
The shell stores the status of the last command in the variable $?, so echo shows 1. The macros EXIT_SUCCESS and EXIT_FAILURE from stdlib.h make the intent readable and portable, since the numeric value for failure is not required to be 1 on every platform.
What If You Forget to Return?
Older textbooks say that without a return statement, a garbage value goes back to the operating system. That was true for the original standard and for many old compilers. Since the C99 standard, if control reaches the closing brace of main in a hosted program, the effect is the same as returning 0. Do not rely on this habit in code that must also compile as C89 or run under unusual tools, and remember that it applies only to main, not to any other function. Writing the return explicitly is the clear option.
return Versus exit
There are two ways to end a program with a status: return from main, or call the library function exit with an integer. In main, return value and exit(value) have the same effect. The difference appears elsewhere: exit can be called from any function, deep inside a call chain, and ends the whole program on the spot after running registered cleanup handlers and flushing open streams.
#include <stdio.h>
#include <stdlib.h>
static void load_config(int ok)
{
if (!ok) {
fprintf(stderr, "config missing, quitting\n");
exit(2); /* ends the program from a helper function */
}
printf("config loaded\n");
}
int main(void)
{
load_config(0);
printf("this line never prints\n");
return 0;
}
config missing, quitting
The helper function ended the program with status 2, and the later printf never ran. A plain return in the helper would only have left the helper.
Command Line Arguments
The second signature gives the program access to the words typed after its name. The value argc is the count of words, including the program name itself, and argv is an array of text strings holding them. By convention argv[0] is the program name, argv[1] the first argument, and so on.
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("program name: %s\n", argv[0]);
printf("argument count: %d\n", argc);
for (int i = 1; i < argc; i++) {
printf("arg %d = %s\n", i, argv[i]);
}
return 0;
}
./show_args alpha 42
program name: ./show_args
argument count: 3
arg 1 = alpha
arg 2 = 42
Notice that 42 arrives as text. To use it as a number you must convert it, for example with a library function such as strtol. Pointers and strings get their own chapters later in this free C programming course.
main in Embedded C Programming
On a microcontroller there is usually no operating system, so nobody is waiting for main to return an exit status. This is called a freestanding environment. Your firmware still needs a starting point, and the structure looks like this:
- Power on or reset: the processor loads its initial stack pointer and jumps to the reset handler address stored in the vector table.
- The reset handler, part of the startup code, copies initialized variables from flash to RAM and clears the uninitialized variable area.
- The startup code may configure clocks and call library initialization.
- The startup code finally calls main.
Because firmware is expected to run as long as the power is on, main normally never returns. It ends with an infinite loop that repeats the application work.
#include <stdint.h>
void board_init(void);
void sensor_poll(void);
void actuator_update(void);
int main(void)
{
board_init();
for (;;) {
sensor_poll();
actuator_update();
}
}
If main did return, control would go back into the startup code, which on many targets simply spins in a loop or triggers a reset. That is why embedded projects frequently place the loop in main and treat falling off the end as a bug. Some embedded toolchains even allow a different entry point name, so check your documentation, but main remains the convention.
Common Mistakes and Troubleshooting
Declaring void main: Many compilers accept it, but it is not portable on hosted systems. Use int.
Calling main recursively: It is allowed in C but forbidden in C++, and it is almost always a design smell.
Treating argv entries as numbers: They are text, so convert and validate them.
Letting firmware main return: The processor then runs whatever follows the call. Add an infinite loop.
Best Practices
- Write int main(void) when no arguments are needed
- Return EXIT_SUCCESS or EXIT_FAILURE on hosted systems
- Keep main short and delegate work to well named functions
- Check argc before touching argv entries
- In firmware, end main with an infinite loop and handle faults deliberately
Summary and Key Takeaways
- Execution starts at main, and there is exactly one main per program
- Its name and signature are fixed by the language
- The int return value is the exit status: zero for success, nonzero for failure
- exit ends the program from anywhere, while return in main does the same from main
- argc and argv carry command line text
- Firmware main usually never returns
You have now finished the theory of the Functions chapter in our free embedded C course. The last lecture is a practice file with a quiz and interview questions.
Frequently Asked Questions
Why does C execution start at main?
The language reserves that name as the single entry point that the runtime calls after setup.
What does return 0 from main mean?
It tells the operating system that the program finished successfully.
Is void main allowed?
Not in standard hosted C. Use int main(void) instead.
What is the difference between exit and return in main?
Inside main they behave the same. Outside main only exit can end the whole program.
Does main return in embedded systems?
Normally no. Firmware loops forever inside main because no operating system waits for a status.
Where can I find a free C course for embedded C programming?
This free C programming course on EmbeddedPathashala teaches C with microcontroller style examples throughout.
Free C Course for Embedded Careers
Finish the Functions chapter with the quiz and interview practice lecture.

3 Comments