The main() Function of C
C programs are composed of
functions
which are vaguely analogous to recipes.
A function can take inputs (like ingredients) and it can
produce outputs (like a cake).
(Don't worry, we won't keep up this analogy for long.)
Now let's return to our "Hello World" program.
It's only function was:
main()
{
printf("Hello World\n");
}
For now, it will suffice to notice only a few things about
function definitions:
-
Every C program must have a function called
main()
.
-
Even though there are more precise ways of defining
main()
, it is enough in simple examples to just follow the
name of the function (
main
) with an empty set of parentheses.
-
The actions that are to make up the program are listed within
a balanced set of braces that follow the line declaring main.
-
While the placement of the braces is not defined by the C
language, we will consistently follow this style and you are
encouraged to do the same.
Joe Inexperienced Hacker tried to write "Hello World" this way:
#include <stdio.h>
Main
{
printf("hello world\n");
{
In what ways did he fail to produce a valid C program?
(Check all the applicable options.)