Compilation process in C program
There are four phases for a c program to be execute,
1.pre-processor
2.compiler
3.assembler
4.linker
let’s start with the first one,
#Pre-Processor- The pre-processor removes the comments and include header files in source code, replace macro name with code.
Basically, it is a program that processes its input data to produce output that can be used by another program as its input. Obviously, compiler is the next one,
#Compiler- A compiler is a program that processes your written data which is in your language convert into machine language i.e. binary form(0 &1).
Basically, it generates the assembly code of the file, it converts the (.i) file into the (.s) file. Any file entering into compiler program passes through lexical analysis, syntax analysis, intermediate code generation, code optimization, object code generation. And the next one is,
#Assembler- An assembler is a program that converts assembly language or assembly code into machine code or object code. Assemblers are similar to compilers in that they produce executable codes.
It converts the (.s) file into the (.o) file. Finally, the last one is,
#Linker- A linker is a computer program that takes one or more object files generated by a compiler and combines them into one, executable program.
There are two types of linker i.e. static linking and dynamic linking.
Static linkingis the result of the linker copying all library routines used in the program into the executable image.
Dynamic linking is done during load or run-time and not when the exe is created.
It converts the (.o) file into the executable file for example (.elf) file.
Now let’s create a simple program,
First we write our program in the vim editor* *(you can use any editor)
#include<stdio.h>
int main()
{
printf(“Hello, This is Ritik Sharma\n”);
return 0;
}
save the file with a name and (.c) extension and then exit from the vim editor
now compile the file with the compiler for example gcc,
then execute the file which is ./a.out and you can see the output as given below,
Hello, This is Ritik Sharma
nice! You got the output. If any error occurred during compiling the program then note that error and try to correct that error.
_THANK YOU_