Every C Program that is in text form goes through a series of transformations to obtain the final executable. The various Phases of this transformation are as follows:
- Preprocessing
- Assembling
- Compiling
- Linking
- Loading to main memory
This Process is illustrated in the Figure 1 below
First we will see how to execute a C program in a GNU/Linux environment. We will be using the GCC/GDB toolchain that comes installed by default in almost all GNU/Linux distributions.
The following video link shows the different phases of the Life Cycle of a C program.
Life Cycle of a C Program – Click to view
Consider the program given below that calculates the area of a Circle given its radius.
// circle.c: Calculate and print the Areas of Circles
#include // Preprocessor directive
#define PI 3.1416
double fnCircularArea( double r ); // Function prototype
int main(void)
{
double dRadius = 1.0, dArea = 0.0;
printf( "\tAreas of Circles\n\n" );
printf( "\t\tRadius\t\tArea\n\t---------------------------------\n" );
dArea = fnCircularArea( dRadius );
printf( "Circle 1%14.3f\t%15.5f\n", dRadius, dArea );
dRadius = 5.0;
dArea = fnCircularArea( dRadius );
printf( "Circle 2%14.3f\t%15.5f\n", dRadius, dArea );
return 0;
}
// The function fnCircularArea( ) calculates the Area of a Circle
// Parameter: Radius of the Circle
// Returns: Area of the Circle
double fnCircularArea( double dR )
{
return PI * dR * dR;
}
Click on the following video link shows how to compile and execute the above C program.
Execution of a C program using gcc compiler – Click to view
TO check which version of gcc is installed
$ gcc --version
To compile a program circle.c and to show all warnings
$ gcc -Wall circle.c
Output Redirection
$ gcc -Wall -o circle.x circle.c
Execution
$ ./circle.x
Now we will see the different phases of transformation. The first phase is Preprocessing. Click on the below link to understand the phase of Preprocessing.
Preprocessing Phase – Click to view
Preprocess and write to circle.i
$ gcc -E -o circle.i circle.c
Preprocess but do not remove comments
$ gcc -E -C -o circle.i circle.c
In the next video link we will see how to perform assembling, compiling and linking.
Assembling, Compiling and Linking – Click to view
During the assembling phase the preprocessed code is converted into assembly code.
Assembly
$ gcc -S circle.c
verbose mode
$ gcc -S -fverbose-asm circle.c
During the Compilation stage the assembled code is converted into object code.
Compile only
$ gcc -c circle.c
The object code is then linked to get the final executable
Linking
$ gcc circle.o -o circle.x
All the phases discussed above can be generated in a single command as follows
$ gcc -Wall -o circle.x circle.c -save-temps
Finally take this quiz that can be accessed at this moodle site. Click on the following Link
http://moodle.sit.ac.in/moodle
Here scroll down to find the course IITB OER QUIZ
Click on it and login name as demouser with Change@1nce as the password to access the course
There in the course under Topic 1 you can find a Demo Quiz. Click and take the quiz