Page 18 - Programming Microcontrollers in C
P. 18
Some Simple Programs 3
After the message is sent to the screen, there is nothing more for
the program to do, so the program is terminated by executing the
statement return 0;. This statement returns the value 0 back to
the calling program, which is the operating system. Also, execution
of the return statement will cause all open files to be closed. If there
were no return statement at the end of the program, the normal pro
cessing at the end of the program would close open files, but there
would be no value returned to the calling program.
This is an area where there is much discussion and many dissent
ing viewpoints. Early C did not require that main return a value to
the calling program. When the C89 standard was written, it required
that main return an int. Unfortunately, many people, set in their
ways, have refused to adhere to the standard nomenclature in this
case and they often use void main(void) instead of the form
above. Most compilers will ignore this form and allow the void
main(void) function call. For some reason, this form angers many
code reviewers, so you should use the correct form shown above.
The program is closed by the inclusion of a closing brace, }, at
the end. There could be many statements within the block following
main() creating a program of any complexity. The closing brace is
the terminator of a compound statement. The compound statement is
the only case in C where a complete statement closure does not re
quire a semicolon.
Another program example is as follows:
#include <stdio.h>
int main (void)
{
int a,b,c,d;
a=10;
b=5;
c=2;
d=a*b*c;
printf(“a * b * c = %d\n”, d);