4.9.1 The main Function
The identifier main
is special. You must always have one,
and only one, function called main()
in your programs. This is the first C
function to execute in your program.
Since your program is not called by a host, the compiler inserts special
code at the end of main()
, which is executed if this function ends, i.e.,
a return statement inside main()
is executed, or code execution reaches
the main()
’s terminating right brace. This special code causes execution
to jump to address 0, the Reset vector for all 8-bit AVR
devices. This essentially performs a software Reset. Note that the state of registers after
a software Reset can be different to that after a hardware Reset.
It is recommended that the main()
function does not end.
Add a loop construct (such as a while(1)
) that will never terminate either
around your code in main()
or at the end of your code, so that execution
of the function will never terminate. For example,
int main(void)
{
// your code goes here
// finished that, now just wait for interrupts
while(1)
continue;
}