Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Compilation Process in C Language – Explained in Detail

Basics of SQL
Basics of SQL
icon
12 Hrs. duration
icon
12 Modules
icon
2600+ Learners
logo
Start Learning

Compiling is a critical step in converting C source code into executable programs. This process converts the source code into human-readable form. Understanding the compilation process is very helpful for programmers to troubleshoot issues, optimise performance, and gain a deeper appreciation for how C programs are executed on a computer. This article explains the compilation process in C language.

What is a Compilation?

It is the process of converting the source code of the C language into machine source code. As we know, C is the middle-level language. It is performed by a compiler, which reads the entire source code, analyses it for syntax and semantic errors, and translates it into a lower-level language. Then, the resulting executable file can be run on the target machine. It involves sewerage stages, including lexical analysis, syntax analysis, semantic analysis, optimisation and source code generation.

How to Compile and Run a C Program?

You need a compiler and a code editor to compile and run a C program in your computer system.

 

  • Step 1: Write Your C program: Create a C source file with .c extension. For example, we can create a file called hello.c with the following source code.

 

# include <stdio.h> int main(){ printf("Hello, Worldn") ; return 0 ; }
  • Step 2: Open your command-line interface (Terminal on macOS/ Linux or Command Prompt / PowerShell on Windows).
  • Step 3: Use the cd command to change the directory  where your program is located
  • Step 4: Run the Program by using the following command.
    gcc hello.c -o hello
  • Step 5: Run the compiled Program using the following command “./hello”

The Compilation Process in C

There are four steps involved in the compilation process.

  • Preprocessing
  • Compiling
  • Assembling
  • Linking

Compilation Process in C Language

Pre-Processing

Compilation preprocessing is a crucial initial step in preparing source code for the compilation process. It involves tasks such as file inclusion, where the preprocessor incorporates external files specified by #include, and macro definitions, which are established using #define to create constants or inline functions. The conditional compilation directives like #ifdef allows source code segments to be included or excluded based on specific conditions, aiding in platform-specific adjustments and debugging.

 

1. Comments Removal

 

In C language, comments give a general idea about a particular statement or part of the source code. Comments are part of the source code that the preprocessor removes during the compilation process as the machine does not particularly use them. The comments in the below program will be removed when the program is in the preprocessing phase.

/* This is a multi-line comment in C */ #include<stdio.h> int main() { // this is a single-line comment in C   return 0; }

2. Macros Expansion

 

The macros are constant values or expressions defined in the C program using the #define directive. Macros defined in the source code are replaced with their corresponding definitions during the pre-processing phase of compilation. Using shorthand notations for repetitive code patterns or contents allows for more flexible and efficient source code.

 

More Example

Defining a value

             #define G 9.8

 

3. File Inclusion

 

File inclusion in C language means adding another file containing some other pre-written source code to our C program during pre-processing. This is done by using the #include directive.

 

For Example

 

#include <stdio.h>

File inclusion in C

 

4. Compiling

 

The C compiling phase uses built-in compiler software to convert the intermediate (.i) file into an Assembly file, which has assembly-level instruction (low-level code). The C compiler translates the intermediate file to make an assembly file to boost the programmer’s performance.

 

Compiling

 

5. Assembling

 

An assembly-level source code (.s file) is converted into an understandable machine source code (in binary/hexadecimal form) using an assembler. Assembler is a pre-written program that translates code into machine source code. It takes basic instructions from an assembly source code file and converts them into binary/hexadecimal source code specific to the machine type.

Assembling

6. Linking

 

It is the process of including library files into our program. Some predefined files contain the definition of the function in machine language, and these files have an extension of .lib. The unknown statements are written in the object (.o, .obj) file that our operating system can’t understand.

Linking

DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Example

The following program displays the Hello Word on the output screen.

#include<stdio.h> int main() { printf("Hello World!"); return 0; }

Output

Hello World

Flow Diagram of the Program

The following diagram is used for the compilation process in C language.

Compilation Process in C Language

 

  • We have a C program file with an extension of .c hello.c file.
  • In the second step, we have to compile the hello. I file. Compiler software translates the hello.I file it with hello.s with assembly-level instructions (low-level code).
  • It is assembly-level source code instructions that the assembler converts into machine-understandable source code(binary/hexadecimal form).
  • A linker links the library files with the object file to define the unknown statements.
  • The linker is used to link the library files with the object file to define the unknown statements. It generates an executable file with the .exe/ .out extension, hello.exe/hello.out.
  • Next, we can run the hello.exe/hello.out executable file to get the desired output on our output window “Hello World”.

 

Also Read: Doubly Linked List Program in C

Conclusion

In this article, we learned about the process in C language. It is a very crucial step that transforms high-level source code into machine-readable source code. This multi-stage process typically includes preprocessing, compiling, assembling, and linking. Each stage serves a specific purpose, such as handling directives and converting source code to an intermediate form, generating object source code, and finally linking various modules and libraries to create an executable.

FAQs
Linking is combining one or more object files into a single executable. It resolves references between modules and includes necessary libraries.
Optimisation can be achieved by compiler flags, organising source code efficiently, reducing dependencies, and minimising includes.
An executable file is the final output of the compilation and linking process that can be run on a computer. It contains machine source code that the CPU can execute directly.
A semantic error occurs when the source code is syntactically correct but logically incorrect, leading to unintended behaviour during execution.
brochureimg

The DevOps Playbook

Simplify deployment with Docker containers.

Streamline development with modern practices.

Enhance efficiency with automated workflows.

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment

Finance

Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management

Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved