#include <stdio.h> | Line 1 |
void main() | Line 2 |
{ | Line 3 |
printf("Hello World\n"); | Line 4 |
} | Line 5 |
A line-by-line dissection.
Line 1: This line tells the compiler
to glue in the contents of the text file stdio.h which is to be
found in the include directory associated with your compiler.
Most compilers set themselves up
on a computer's disk in a particular way. C-compilers traditionally
set up a directory tree as follows:
The bin directory contains any exectuble files associated with
the compiler (such as the compiler program
itself). The lib directory contains any libraries
of functions which are available for use by program you may
develop. The include directory contains files which include
declarations of constant, functions and other items
which facilitate compilation. One of these files is stdio.h.
This file contains declarations of such functions as
printf, scanf and so on.
Line 2: This line states that what follows is a function called main. It takes no parameters and returns nothing.
Line 3: This is simply the opening brace for all of the statements within main.
Line 4: This statement represents a function
call to a function called printf. It is important to note
that the
compiler has no built-in knowledge of this function as it is not a
C-language keyword. How does it know what
to do with it then? How is it to know that you are allowed pass
a string as a parameter? The answer to
both of these questions lies in the include file stdio.h.
This file contains a forward-declaration (or prototype)
of the function printf which informs the compiler of the permissible
parameters. If you had not included the
file stdio.h, the compiler would either signal an error or warning
when it encountered printf.
Line 5. This is the closing brace for the function main.
When the compiler compiles this program, it opens up the stdio.h file
and compiles all within it. Mostly this
process will consist of noting function prototypes and other symbol
definitions. The compiler then proceeds to
compile each line of the Hello World program, converting all relevant
C keywords into equivalent machine code
constucts. It also builds up a table of symbols which it will
pass on the linker. One of these symbols will be _printf.
(By the way, you might be wondering why there is a leading underscore
in front of each of the functions names.
These are included because, by convention, C-compilers pre-pend an
underscore to all symbols which they
process)