A compiler translates the C program (source code) into machine language (machine code) which it stores on the disk as a file.
This process of converting the source code into machine code is known as compilation.
Stages of Compiling a C Program
There are 4 stages in the process of compilation of a C Program.
Preprocessing is the first stage in the process of compilation. In this stage:
- Preprocessor commands that begin with
- Comments are removed from the source code.
- Macros are expanded.
In this stage, the preprocessed code is translated into assembly code and syntax errors are checked by the compiler.
In this stage, an assembler is used to translate the assembly code into object code (machine code).
Linking is the final stage in the process of compilation. In this stage, all the object code from multiple modules is merged (linked) together and creates an executable image which is also saved on the disk, usually as a file with the file name without any extension (e.g. hello), so that it can finally be executed by the processor.
gcc –Wall hello.c
This compiles a file named hello.c and the output file will be named a.out.
gcc –Wall hello.c –o hello
This compiles a file named hello.c and specifies the output file name as hello. The option
-o is used to name the output file.
The executable file hello is loaded from the disk to memory and the computer’s CPU (Central Processing Unit) executes the program one instruction at a time.