Compilation in C

Compilation in C

A compiler is a program that translates a code written in one programming language (C in this post) into machine language, which is made of ob. We will use gcc as the compiler.

Let's start with a C program that will print "Hello World" and I will name it "hello".

This is how the file will look like

hello.c

And this is how the C instructions will look like

#include <stdio.h>
/**
 * main - Entry point
 *
 * Return: Always 0 (Success)
 */
int main (void)
{
    printf("Hello World\h");
    return (0);
}

To compile this program and translate it to machine code so it can be excecuted, I will use gcc

$ gcc hello.c

This will initiate the compilation process, which is divided in four steps:

  1. Preprocessing: Is the first stage of compiling and it removes the comments and include header files in sorce code.
  2. Compiling: Once the code is free of comments, the compiling process translate it into assembly language (ASM), a low level language closer than C to the Machine Languange.
  3. Assembly: It translate the code that at this point is in assembly language, into object code, that contains a sequence of machine-readable instructions.
  4. Linking: Is the creation of a single executable file from multiple object files created in the previous step. The result of this stage is the final executable program or library.

To view or add a comment, sign in

More articles by Andrés Ortiz

  • What happens when you type `ls -l *.c` in the shell?

    Introduction: If you have ever used a linux shell, you probably already know what ls is and does, but if not, let me…

  • C Static Libraries

    While we are writing programs, we will face the fact that many functions are used in more than one program, or, that…

Explore content categories