The compilation is the process of converting the source code of the C language into machine code. As C is a mid-level language, it needs a compiler to convert it into an executable code so that the program can be run on our machine.

The C program goes through the following phases during compilation:

compilation process in c

How do we compile and run a C program?

We first need a compiler and a code editor to compile and run a C Program. The below example is of an Ubuntu machine with GCC compiler.

Step 1: Creating a C Source File

We first create a C program using an editor and save the file as filename.c

 $ vi filename.c

We can write a simple hello world program and save it.

Step 2: Compiling using GCC compiler

We use the following command in the terminal for compiling our filename.c source file

 $ gcc filename.c –o filename

We can pass many instructions to the GCC compiler to different tasks such as:

  • The option -Wall enables all compiler’s warning messages. This option is recommended to generate better code. 
  • The option -o is used to specify the output file name. If we do not use this option, then an output file with the name a.out is generated.

If there are no errors in our C program, the executable file of the C program will be generated.

Step 3: Executing the program

After compilation executable is generated and we run the generated executable using the below command.

 $ ./filename

The program will be executed and the output will be shown in the terminal.

c file compilation and execution