←to practical programming

Structure of a C-program and basics of compilation

Function

A function in C is a block of code within a pair of curly brackets, {...}, with a unique name and a declared type. The function must return a value of the declared type unless the type is void in which case it does not return anything. A function can optionally take arguments.

For example, here is a function that takes two double arguments and returns a double value,

double multiply(double a, double b)
{
double r = a*b;   /* do some useful stuff */
return r;         /* return a value of declared type */
}
Here is a function that takes no parameters and returns nothing,
void hello(void){ printf("hello, world\n"); }
A function is called by its name followed by parenthesis with the arguments (if any), for example,
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.

C-program

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

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 (often in separate header files with extension .h), and also preprocessor directives.

Object code and compilation

The object code is the translation of the function definitions from the C-language into the 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 option means compile only and -o hello.o means output goes into file "hello.o". (The last option is not strictly necessary as the file hello.c is compiled into hello.o by default).

Preprocessing

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 macros and do other useful tricks.

For example, after the preprocessor directive

#define PI 3.1415927

all the instances of the token "PI" will be replaced with the token "3.1415927".

Executable code and linking

The executable code is the ready program: the object code of all functions 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 usually called using the gcc command. Given the object code, gcc does linking of the supplied object file(s). For example, the command

gcc hello.o -o hello -lm

links the functions in the file "hello.o" and writes the resulting executable code into file "hello". Here -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). All other standard libraries are linked by default except for the math library which has to be linked explicitely (it's historical, like a.out).

By default the exectable code is put into a file with the name a.out (for historical reasons).

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 according to 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.