codeintuition-logo

Compilation in C++

Learn all about compilation in C++ and its advantages

10 minutes
Easy
Beginner

What you will learn

What does a simple C++ program look like

What is a complier and how does compilation work

What is an interpreter and how does it work

What is the difference between a compiler and an interpreter

What is a high level programming language

What is a machine language why is it difficult for humans

Compilers and interpreters are the most essential part of the entire software ecosystem. These are magical softwares that make it possible for humans to talk to bare mental chips (computers). In this article, we will take a high-level look at what compilation really means and what other options we have if we want to talk to a computer.

High Level Languages

To understand better how compilation in C++ works, let us look at a very basic "Hello World" C++ program.

  1. Hello World

The C++ code we wrote is in a language that we as humans can understand and so C++ is also sometimes categorised as a high-level language.

High level languages
High level programming languages are the ones which are closer to human understandable languages.
With the advent of even higher level languages like Javascript, Python, etc. C++ is now often categorised as a medium level language

Computers on the other hand are just chips made of silicon and so it is not possible for them to directly understand such high-level languages. How does our C++ program work then? To understand this better let us understand first how does a computer system actually work.

Central Processing Unit

We don't need to get into too much detail about how a CPU (Central Processing Unit) works as it would make up an entire article in itself. For simplicity, you can imagine the central processing unit as a very complex chip. Let us look at how the CPU looks like -

Loading Image

A very simple processor

Every CPU comes with a manual from the manufacturer that has the full set of operations that a CPU can perform like addition, subtraction, loading and storing values to and from the RAM(memory), etc. A user can instruct the CPU to perform these operations by passing the corresponding instructions to it. These instructions are just a sequence of high(1) and/or low(0) voltage applied across the right set of pins as specified in the user manual of a CPU.

What this really means is at the root level computers only have a finite set of operations that they can perform. These operations make up the machine language for a computer and the mode of communication of this language is digital signals in the form of 0s and 1s.

Machine Language

Machine language is the language that is understood by hardware components of a computer system like the CPU.

Machine Language
Machine language or machine code is the language comprising of 0s and 1s that machines understand

For a computer to understand the code that we write in high-level languages like C++, all the instructions in our code need to be somehow be converted to machine language. This magic is done by translating softwares that convert instructions in high-level languages to machine language. 

The major categories of these translating softwares are -

  • Compilers
  • Interpreters

Interpreter

An interpreter converts our code in a high-level language to a machine language one line of the code at a time. Once a line is converted to machine language, it gets executed before the interpreter moves on to the next line.

Loading Image

Working of an Interpreter

Some examples of compiled languages are -

  • Python
  • JavaScript
  • Pearl
  • BASIC

Compiler

A compiler converts our code in a high-level language to a machine language all lines of the code at once. Once the compilation is done we get a single file containing the machine code that has all our instructions in the high-level language covered to equivalent instructions in the machine language. This machine code can then be directly fed to the CPU to execute our program

Loading Image

Working of a Compiler

Some examples of compiled languages are -

  • C
  • C++
  • C#
  • Go

So as you can see, C++ is a compiled language. This means that all the code that we write serves as the input to the C++ compiler which then converts it to machine code for our CPU

Do you want to master data structures?

Try our data structures learning path made of highly visual and interactive courses. Get hands on experience by solving real problems in a structured manner. All resources you would ever need in one place for FREE

Shikhar Srivastava

Shikhar Srivastava

London, United Kingdom