Programs

What is pre-processing in C? 

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!

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.

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.

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.

Want to share this article?

Be a Master in Software Development

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks