Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Compiler?
3.
What is a Preprocessor?
4.
Difference between Compiler and Preprocessor
5.
Frequently Asked Questions
5.1.
What is the difference between Compiler and Preprocessor?
5.2.
Is Preprocessor part of the compiler?
5.3.
What are the types of Preprocessor tasks?
6.
Conclusion
Last Updated: Mar 27, 2024

Difference between compiler and preprocessor

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

As we all know, computers can only understand machine language, i.e., codes containing 0s and 1s. We in daily write many programming codes for various computer programs, and you may think about how the computer executes these codes. The answer is that our codes are converted to machine code by computer, and the compiler and preprocessors handle these translation processes. Let’s learn about these in detail.

Difference between compiler and preprocessor

This blog will explain the difference between compiler and preprocessor and how they execute codes.

Recommended Topic, loop and while loop

What is a Compiler?

A compiler translates a programming code into machine-readable code so that the computer can execute it. It converts all the files of a given task at once. The program code goes through various steps during the compiling process. Let's discuss these steps in detail.

Steps during compiling process
  • Lexical Analysis
    The Lexical Analysis is the first step. The compiler reads the source code and splits it into many meaningful units known as lexemes. These are then tokenized for further steps. It also removes comments and white spaces.
     
  • Syntax Analysis
    The code goes through the next step, syntax analysis. As the name suggests, the compiler checks whether the code is valid according to the programming language's grammar. It verifies the syntax by creating a Parse tree. If the source can be produced with the help of the tree, then it is in the correct syntax.

 

  • Semantic Analysis
    The next step is semantic analysis. During this, the compiler verifies whether the program makes sense and checks for errors. For example, if a variable is used before its declaration then the compiler will return an error message.
     
  • Intermediate Code Generation
    After all the analyses, the compiler modifies the code. It translates the source code into an intermediate code.
     
  • Code Optimization
    The intermediate code generated must be optimized before the final translation. This step improves intermediate code so the output machine code can be executed smoothly.
     
  • Code Generation
    Code generation is the final step of compiling phase. In this step, the compiler generates the required machine-readable code for execution from the intermediate optimized code.

 

Let’s now see what the functioning of a preprocessor is and how it works. We will also discuss the difference between compiler and preprocessor.

Must Read Recursive Descent Parser , cousins of compiler and Cross Compiler

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is a Preprocessor?

The preprocessor processes the commands in source code before forwarding it to compiler for compiling process. A hash symbol (#) is used to initialize the process of preprocessor commands in C++. There are various preprocessor commands. Let’s take a look at these commands.

Different preprocessing commands
  • Header file inclusion
    The header file contains different variable declarations and functions used in the source code. The preprocessor adds the content of these files into the source code. For example, in C++, #include<bits/stdc++.h> will be replaced by the libraries and functions present in bits/stdc++.h module.
     
  • Macro Expansion
    Variables that will have constant values are predefined so that they can be used throughout the code. Whenever the preprocessor encounters that variable, it will replace it with the value defined. For example, in Cpp, #define length 9. Before compilation, the preprocessor will replace the “length with 9 in the source code.
     
  • Conditional compilation
    It lets us decide which part of the source code should compile based on certain conditions. Some conditional compilation directives are #if, #elif, etc.

Difference between Compiler and Preprocessor

We have discussed compilers and preprocessors and their commands. Let's look at the difference between compiler and preprocessor for better understanding.

Compiler

Preprocessor

An input for a compiler is a preprocessed code ready for the compiling process. An input for a preprocessor is a source code in any programming language.
It is a computer program that converts source code into machine code. It is a computer program that processes the source code before it is compiled.
It runs after the preprocessor has completed its task. It runs before the compiler and modifies the source code for the compiler.
Its output is a machine-readable code ready for execution. Its output is an improved source code ready for compiling.
It performs lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation tasks. It performs header file inclusion, Macro expansion, and conditional compilation tasks.
It is used to compile program files of various programming languages. It works on the source code of a specific programming language.
It also performs error detection and optimizes the program for better performance. It does not perform any error detection or correction on the source code.

Related Article Compiler and Preprocessors in C.

Frequently Asked Questions

What is the difference between Compiler and Preprocessor?

The preprocessor modifies the source code for the compiler. It performs different commands. The compiler translates this preprocessed program into machine-readable code. Both are two different tools but are used together to execute a code. 

Is Preprocessor part of the compiler?

No, the preprocessor is not a part of the compiler. It improves the source code by including the header file’s content, constant variable values, etc. This helps the compiler during the compilation.

What are the types of Preprocessor tasks?

Three preprocessor tasks are macro expansion, header file inclusion, and conditional compilation. These tasks modify the source code for the compiler. This makes it easier to maintain large programming codes.

Conclusion

We hope this article was insightful and you learned something new. This blog taught us how computers convert your codes into machine code for execution. The compiler and preprocessor are used for these translation processes. We also saw different tasks that are performed during these processes. In the end, we concluded with the difference between compiler and preprocessor.

You can also check the following links to learn more

Do visit our website to read more such blogs. Refer to our Guided Path on Coding Ninjas Studio to upskill yourself! You can check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! If you are looking for questions asked by tech giants, in that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.
 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!!

Previous article
Compiler vs Cross Compiler
Next article
Handle Pruning in Compiler Design
Live masterclass