Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

What is pre-processing in C?

Updated on 26 September, 2022

7.93K+ views
7 min read

Preprocessing in C refers to the essential steps performed before the compilation of a C program. To understand this better, take the example of following through a recipe and preparing a dish. In that case, mixing all the ingredients and preparing the dish is processing, whereas collecting all the ingredients in the right amount, performing all the pre-preparatory steps come under preprocessing. 

Extending this forward, we are going to discuss various concepts and essential nuances of preprocessing in C. 

Preprocessors in C

The word ‘preprocessor’ can be broken down as ‘pre’, meaning ‘before’, and ‘processor’, meaning ‘making something’. A program is automatically preprocessed using various preprocessing directives in C before it is compiled in the C programming language. 

In essence, preprocessors are essentially system software that processes high-level language. For this, the high-level code is converted to object code, written in the machine-level language. In doing so, preprocessors make it easier for compilers to understand the code since compilers don’t work with high-level languages but with machine-level instructions.

The C source code that you write will be saved in the following format – <filename>.c. Here, <filename> will be the name of your source code file, and .c is the extension. The contents of this file, written by you, are in a high-level language. So, first, preprocessors work on the .c file and expand the source code. Once this is done, the code is compiled, and an object file is produced of the following format – <filename>.obj. This object file is linked to the Standard Library Functions, and a final .exe file is generated that can be executed on your system by simply double-clicking the file. 

Before we dive deeper into the various types of preprocessor directives available with C, let’s first look at what happens when we don’t include any preprocessors in our code. If you’ve written even one C program, you’ll know that it starts with the #include statement. 

These are called header statements. If you miss adding the pound (#) symbol before your include statement, you’ll receive an error message. Try running the following code in your C IDE. 

#include<stdio.h> 
void main()
{
printf(“Welcome to upGrad blogs!\n”);
}

In this case, the preprocessor in C cannot process the include<stdio.h> line since it isn’t following the hash symbol. If we’d used the hash symbol, the C preprocessor would have first expanded the #include statement and incorporated all the required header files for our program to function. In the absence of that, we receive an error, and the program doesn’t work. We understand that the hash symbol is indeed quite important to keep the code error-free and properly running. 

Talking about the history of preprocessors in C, they were introduced in the 1970s. The original preprocessors available with C allowed programmers only to add files and perform simple string replacement operations. With time, more preprocessor directives were made available in C, and today, we have many kinds of preprocessor directives in the C programming language. Let’s look at the different types of preprocessing in C!

Different Preprocessor Directives in C

Put, preprocessing directives are your program statements that start with the hash (#) symbol. The hash symbol is then followed by the directive’s name, which acts as an identifier. For instance, #define is the directive that defines a macro. Note that the C programming language allows whitespaces before and after the # symbol. 

C programming language provides the following preprocessor directives, all of which start with the hash symbol. 

1. #include

This preprocessor directive is used for pasting a code of a given file (that is being included) into your current file. #include directive can use both user-defined as well as system-defined headers. If you mention a file that doesn’t exist, the compiler will return an error. #include has three variants: 

  • #include <file>

This is used for including system-defined header files in your piece of code. This statement searches for the filename mentioned by you in the list of directories first and then in the standard list of system-defined directories. 

  • #include “file”

This is used to include header files of user-defined code. This statement will first search for the filename in the current directory of your program code, then in the same directory as other header files. 

  • #include anything else

The third variant is known as a computed include. Any #include that does not fit in the above two categories is a computed include statement. 

2. #define

#define is used for defining macros. A macro can be understood as a piece of code that is replaced by the macro’s value that you have defined. To add macros, you need the #define preprocessor directive. 

The syntax of the #define preprocessor directive is – #define tokenvalue

#define token value  

Further, there are two types of macros: 

  • Object-Like Macros

This macro is an identifier that is replaced by a constant value. This is majorly used to represent numeric constants. For example, #define PI 3.141 will automatically define the value of PI as 3.141. 

Let’s see this in action once: 

#include <stdio.h>  
#define PI 3.14
main() 
{  
   printf(“%f”,PI);  
}  

Output:

3.1400

  • Function-Like Macros

These macros look like a normal function call. For instance: #define MINIMUM(x,y) ((x)<(y)?(x):(y))    

Here, MINIMUM is the macro name. Let’s see this in action: 

#include <stdio.h>  
#define MINIMUM(x,y) ((x)<(y)?(x):(y)))  
void main() {  
   printf(“min of 5 and 7 is: %d\n”, MINIMUM(5,7));    
}  

Output:

Min of 5 and 7 is: 5

3. #undef

Undefining a macro means cancelling its definition. So, this preprocessor directive is used to cancel the definition of any previously defined macro. The syntax is #undef token.

Let’s look at an example to understand this better: 

#include <stdio.h>  
#define PI 3.14  
#undef PI  
main() {  
   printf(“%f”,PI);  
}  

Running this program will give you a Compile Time Error since the value of PI has been undefined as soon as you used the #undef directive. 

4. #ifdef

This directive checks if a particular macro has been defined using #define or not. If it has already been defined, this directive will execute the code. The syntax is: 

#ifdef MACRONAME  
//code to run
#endif

5. #ifndef

This directive works opposite to the #ifdef directive. This directive first checks if the macro is not defined by using #define. If it isn’t, it executes the code. The syntax is: 

#ifndef MACRONAME
//code to run
#endif  

6. #if

This preprocessor directive evaluates the condition or the expression. This works like the if-else conditional statements in C, so if the condition is true, the code mentioned will be executed. The syntax: 

#if expression  
//code to run  
#endif  

7. #else

This preprocessor works if the conditioned mentioned in the #if directive is not met. #else can be used with #if, #ifdef, #ifndef, and #elif. The syntax is: 

#if expression  
//code for if  
#else  
//code for else  
#endif

Syntax of #else with #elif:

#if expression  
//code for if  
#elif expression  
//code for elif  
#else  
//code for else  
#endif  

Let’s understand this better using an example: 

#include <stdio.h>  
#include <conio.h>  
#define NUM 2  
void main() {  
#if NUM==0  
printf(“Num is: %d”,NUM);  
#else  
print(“Num is non-zero”);  
#endif         
getch();  
}  

Output:

Num is non-zero

8. #error

This preprocessor directive is used to indicate errors. The compiler returns a fatal error if the #error directive is found. Also, once this directive is found, the compiler skips the further compilation process soon after raising the error.

9. #pragma

This preprocessor directive is used to provide additional information to the C compiler. It offers machine or operating-system features to the compiler. Different compilers have different usage of the #pragma directive.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

In conclusion

The concept of preprocessors in C is extremely important to understand. Preprocessor directives can be used in multiple ways. The examples we saw above are by no means exhaustive. This is to give you a working understanding of how preprocessors in C work and what different types of preprocessors are available with C. We hope this article helped you understand the basics required to perform advanced computations with preprocessors in C. 

C programming language is known as the mother language and for all the right reasons. Once you have mastered C, all other programming languages will come pretty much naturally to you. upGrad offers you a Full Stack Development course from IIITB that helps you master all the essential skills required to get started with and ace your software development journey. Check out the course content and get yourself registered today!

Frequently Asked Questions (FAQs)

1. Apart from the C language, what all languages have preprocessors?

All the languages that are compiled and not interpreted require preprocessors. This includes famous programming languages like C, C++, Java, and more. However, keep in mind that since Python, JavaScript, etc., are all interpreted languages, they don’t have preprocessors or preprocessor directives.

2. What are preprocessor directives in C?

Preprocessor directives are simply the commands used in the preprocessor. All the preprocessor directives in the C programming language begin with the hash symbol.

3. Is it necessary to use preprocessors at the beginning of the program?

Some preprocessor directives like #include that are used to include essential files should be used at the beginning of your C program. However, this isn’t a hard and fast rule, and you can use directives anywhere in the code. That said, you should try to add all the directives at the beginning of your code to keep it neat and more readable.

RELATED PROGRAMS