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:

   printf( "Hello World\n" ) ;

For now, it will suffice to notice only a few things about function definitions:

  1. Every C program must have a function called main().
  2. 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.
  3. The actions that are to make up the program are listed within a balanced set of braces that follow the line declaring main.
  4. 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.

Bubba Joe Bob tried to write "Hello World" this way:

   printf( "hello world\n" ) ;

In what ways did he fail to produce a valid C program?

  1. He capitalized main.
  2. He failed to capitalize hello world.
  3. He forgot the parentheses.
  4. He didn't balance opening braces with closing ones.
  5. He misspelled print.

Answer (a)    Answer (b)    Answer (c)    Answer (d)    Answer (e)    Answers (a, c, & d)