A computer is an electronic machine. It does not have a brain like humans. It cannot understand feelings, ideas, or natural languages.
A computer understands only one language, which is called:
Machine Language
All programs written in C++, Java, Python, or any other language must finally be converted into machine language before the computer can execute them.
Machine Language
What is Machine Language?
Machine language is the lowest-level programming language. It is the only language that a computer can understand directly.
Machine language is written using:
0 (zero)
1 (one)
These are called binary digits or bits.
Example of machine language:
10101010 00010101 11001010
To humans, this looks meaningless, but to a computer, it is a complete instruction.
Why Does Computer Use 0 and 1?
Computers are made of electronic circuits. These circuits have two states:
ON
OFF
ON is represented by 1
OFF is represented by 0
That is why computers understand only binary numbers.
Problems with Machine Language
Although machine language is fast, it has many problems:
Very difficult to read
Very hard to write
Easy to make mistakes
Different for different computers
Because of these problems, programmers do not write programs in machine language.
High-Level Languages and Translators
To make programming easier, humans write programs in high-level languages like C++.
But computers cannot understand high-level languages directly.
So, we need a translator that converts high-level code into machine language.
There are two main types of translators:
Compiler
Interpreter
Compiler
What is a Compiler?
A compiler is a program that translates the entire source code into machine language at once.
In simple words:
A compiler converts the whole program into machine code before execution.
C++ uses a compiler.
How Compiler Works
Programmer writes a program in C++
Compiler checks the whole program
If there are errors, it shows them
If there are no errors, it creates an executable file
The executable file can be run many times without recompiling.
Advantages of Compiler
Fast execution
Errors found before execution
Program runs independently
Disadvantages of Compiler
Errors are shown after checking the whole program
Takes more time to compile large programs
Interpreter
What is an Interpreter?
An interpreter translates the program line by line.
In simple words:
An interpreter reads one line, translates it, executes it, and then moves to the next line.
Languages like Python and JavaScript use interpreters.
How Interpreter Works
Programmer writes the program
Interpreter reads the first line
Translates and executes it
Moves to the next line
If an error occurs, execution stops immediately.
Advantages of Interpreter
Easy to debug
Errors shown one by one
Good for beginners
Disadvantages of Interpreter
Slower execution
Needs interpreter every time program runs
Compiler vs Interpreter
| Feature | Compiler | Interpreter |
|---|---|---|
| Translation | Whole program | Line by line |
| Speed | Fast | Slow |
| Error Detection | After compilation | During execution |
| Example Languages | C, C++ | Python, JavaScript |
Source Code and Executable Files
Source Code
Source code is the program written by the programmer in a high-level language.
In C++, source code files usually have the extension:
.cpp
Example:
main.cpp
program.cpp
Source code:
Is readable by humans
Cannot be understood by the computer directly
Executable File
An executable file is a machine language file created by the compiler.
Examples:
.exe (Windows)
a.out (Linux)
Executable files:
Are understood by the computer
Can be run directly
Are not easy for humans to read
C++ Compilation Process
The C++ compilation process explains how a C++ program becomes an executable file.
Step 1: Writing the Source Code
The programmer writes a C++ program using a text editor or IDE.
Example:
#include <iostream>
using namespace std;
int main() {
cout << “Hello World”;
return 0;
}
This file is saved with the extension .cpp.
Step 2: Preprocessing
The preprocessor:
Handles #include statements
Handles macros
Removes comments
Step 3: Compilation
The compiler:
Checks syntax errors
Converts code into object code
If there are errors, compilation stops.
Step 4: Linking
The linker:
Combines object files
Links libraries
Creates the final executable file
Step 5: Execution
The executable file is run, and the computer executes the program in machine language.
Why Understanding This Process is Important
Understanding how a computer understands programs helps students:
Debug errors easily
Write better programs
Understand compiler messages
Become better programmers