C Programming Language
How C Code Gets Compiled: Preprocessor to
Executable
Preetam Sir
(Microsoft Cybersecurity
Architect)
What is programming?
The term programming means to create (or develop)
software, which is also called a program.
Computer programs, known as software, are instructions
that tell a computer what to do.
Every Computer language is designed for some specific
purpose. For example, Fortran was designed for scientific and
mathematical calculations, COBOL (Common business
Oriented Language) was designed for business applications.
Similarly, C language was developed for programming in the
operating system called UNIX.
1
Why do we use C?
C language is used to create applications or
software.
Initially, C was developed to create an operating
system called UNIX.
The popular software like Linux OS, PHP & MySQL
are created using C language.
2
What is c?
C is a middle-level and general-purpose programming
language that is ideal for developing firmware or portable
applications.
Understanding C Language and English
Language :
Instead of straight-away learning how
to write programs, we
must first know what alphabets,
numbers and special symbols
are used in C, then how using them
constants, variables, and
keywords are constructed, and finally
how are these combined
to form an instruction? A group of
instructions would be
combined later on to form a program.
3
What is Program Compilation?
Compilation is the process of translating high-level source code into
low-level machine code
•The output of compilation is
binary code (machine-readable
format).
•Computers can only execute
instructions written in binary due
to their hardware architecture.
Therefore, any program written
in a high-level language must be
converted into machine code.
This machine code is what the
processor understands and
executes.
4
Program Compilation Stages
Compilation Stage Accepts Input Gives Output Result
Resolve #include,
1. Pre-processing Program Source Code Pre-processed Code expand #define,
remove comments
Assembly Code Compiler produces
2. Compilation Pre-processed Code
Instructions Assembly Code
Assembly Code Assembler produces
3. Assembly Object Code File
Instructions Object Code
Single Machine Code Linker links and
4. Linking Object Code Files
File produces Executable
4
What is Pre-Processing Stage?
The first stage of the compilation
process is called the
preprocessor stage.
•It is also referred to as the
lexical analysis stage.
Takes the program source code file as
input.
The compiler scans the source code
file for all preprocessor directives,
such as:
•#include
•#define
4
What is Compilation stage
Compilation is the second stage of the program compilation process.
The compiler accepts the preprocessed file as input.
It provides the assembly code as output with a .s
extension.
Converts high-level program instructions into equivalent assembly
code instructions.
These assembly instructions are
platform dependent and
compiled for a specific
architecture
4
What is Assembly Stage
An assembler is used to convert this assembly code into object code
(machine-level instructions).
The output of this stage is an Object File, usually with a .o or .obj extension.
The object file contains machine code, but it is not yet executable.
4
Linking Stages
Linking is the final (fourth) stage of the
compilation process.
The main function of linking is to:
•Produce a single executable file
•This is done by linking all object code files together.
If errors occur during this stage (like
unresolved symbols), the linker throws link-
time errors.
The Linking Stage is the final step in the program compilation
process. It takes the object files and combines them into a
complete, executable program.
It resolves external references, such as function calls or variables
defined in other files.
The linker allocates memory and sets
up the final machine code layout. 4
How Computer Program are Compiled?
4
First C Program
#include <stdio.h> // Preprocessor directive
int main()
{ // Main function - program execution starts here
printf("Hello, World!\n"); // Output function
return 0; // Exit the program successfully
}
4
C Token’s
C Tokens: Character set, Identifiers, Keywords, constants, Data types, type qualifiers,
Declaration and Initialization of variable
Token: The smallest individual units in a program are called tokens.
The ‘C’ tokens are classified as:
1. Character set
2. Keywords
3. Identifiers
4. Constants
5. Operators
4
Character set: Character set consists of___________________
i) alphabet from A-Z or a-z
ii) digits from 0-9
iii) Special characters like (,), {,}, [,], , &, $, #, %, ^, !, ?, :, ;, ",’, .
iv) White space character: blank space
#include <stdio.h>
int main() {
int a = 5, b = 2;
char ch = 'A';
printf("Char: %c\n", ch); // Letter
printf("Sum: %d\n", a + b); // Digits & Operators
printf("Escape: \\ \" \n"); // Special characters & escape sequences
return 0;
}
Output:
Char: A
Sum: 7
Escape: \ "
Hello World is just the beginning
-Preetam Sir