Structure of a C-program and basics of compilation

C-program

A C-program is basically a collection of functions where each function performs certain (useful) tasks. One of the functions has the name main. This function is called by the operating system when the program is run. The main function then typically calls other functions in the program.

Functions
A function in C is a block of code within a pair of curly brackets with a type (can be void) and a unique name. A function can (optionally) take certain arguments of specified types and can (optionally, if not void) return a value of the specified type,
return_type function_name(argument_type_1 argument_1, ..., argument_type_N argument_N)
{
/* calculate the return value */
return value_of_return_type;
}
for example,
double multiply(double a, double b)
{
double r = a*b;   /* do some useful stuff */
return r;         /* return a double as declared */
}
void hello(void){ printf("hello\n"); }
A function can then be called by its name followed by the arguments, if any, in parenthesis,
hello();
double x = multiply(2.0, 4.0);

C is a free form language: you can put (multiple) new-lines, spaces, and tabs anywhere between the tokens of the language.

Source code

The collection of function definitions in a program in the C-language is called the source code. The source code is typically contained in one or several text files with extension .c such as hello.c. Apart from definitions of functions the source code can also contain declarations of variables and types, and also preprocessor directives.

Object code

The object code is the translation of the function definitions from the C-language into processor instructions language. The object code is typically contained in a binary file with extension .o. The process of translation is called compilation and is typically done by a C-compiler such as gcc. For example,

gcc -c hello.c -o hello.o

where -c means "compile only" and -o hello.o means "output (the resulting object code) into the file 'hello.o'". (The last option is not strictly necessary as the file hello.c is compiled into hello.o by default).

Actually before compilation the C-compiler first applies the C-preprocessor to the source code. C-preprocessor is a macro-processor used to include files, define macro-definitions and do other useful tricks.

Executable code

The executable code is the ready program: the object code together with all the necessary library functions and some extra information needed to run the program. Building the executable code from the object code (and the libraries) is called linking. Linking is performed by a linker which is included in the gcc system and is called using the gcc command. For example,

gcc hello.o -o hello -lm

where -o hello means "output the resulting executable code into the file 'hello'" and -lm means "link with libm math library" (in case the program uses math functions). By default the exectable code is put into a file with the name a.out (for historical reasons). When given the object code, gcc calls the linker on the supplied object file(s).

The program can now be run using the command ./hello where ./ means "the current directory".

If the program is wholly contaned in one .c file, like hello.c, the gcc can compile and link it in one go, for example:

gcc hello.c -o hello

If the object code of a program is contained in several files, say math.o and hello.o, and the program also uses functions from a library, say the libm mathematical library, you have to link everything together:

gcc math.o hello.o -o math -lm

The main function

The main function must be defined following one of the following prototypes:

int main() {...} /* no arguments to main */
int main(void) {...} /* no arguments to main */
int main(int argc, char *argv[]) {...} /* arguments in the array-of-arrays argv */
int main(int argc, char **argv) {...} /* the same as above */

The main function must return integer zero upon succesful completion.