What Are Preprocessor Directives In C?

By | October 27, 2023

Varun Saharawat is a seasoned professional in the fields of SEO and content writing. With a profound knowledge of the intricate aspects of these disciplines, Varun has established himself as a valuable asset in the world of digital marketing and online content creation.


What Are Preprocessor Directives In C

Preprocessor Directives In C are statements generally starting with a ‘#’ hashtag symbol that is c preprocessor directives before the compilation begins. They are responsible for adding header files, macros, libraries and modules before the primary function. #Macros, #include, #conditionals (IF, else, elif) are some of the critical preprocessor directives. Let us start with some of the most commonly used preprocessors in C.

Preprocessor in C

A block of statements processed before the start of the compilation is called a Preprocessor in C. They are not included in compilers. They are usually kept at the beginning, however, they can be used anywhere in the program.

  • Firstly, the compiler starts to process our source code.
  • It checks if there are any c precompilers included in the program.
  • If present, it will execute the preprocessor in C  first and then go to the compilation. 
  • Finally, the code gets executed.

Preprocessor in C

full stack web development
full stack web development

Types of C Preprocessor Directives 

Let us check out some of the commonly used preprocessors in C below.

1. #include

These c programming preprocessor directives are used to include essential contents inside the program. These statements generally start with a symbol (#). They are generally used inside the program and included in the C header file.

#include: C Preprocessor Directives 
#include <stdio.h>

2. #Define 

These directives are used for defining Macros. Macros are used to represent any constant value or code snippets. The values used in the macros are replaced before compilation.

#define: C Preprocessor Directives 
#define PI 3.14159

3. #ifdef, #else #endif #ifndef

These preprocessor directives are used for conditional statements in C. They are statements which are used to decide whether to exclude a part of the code or to include them.

#if #else #endif: preprocessor in C 
#ifdef DEBUG

// Code included only if DEBUG is defined

#else

//code is not included 

#endif

4. #error

This preprocessor in C generates a compilation error message during code processing. It is often used in conditional compilation.

#error: preprocessor in C 
#ifdef LINUX

// Code for Linux

#else

#error This program requires a Linux environment.

#endif

5. #undef

This preprocessor in C is used to undefine any previously defined macro in our program. 

#error: preprocessor in C 
#define PI 3.14

#undef PI

6. #pragma

Pragma is used to provide additional information to the compiler. They are not a part of the C language; however, they are used for optimization purposes. 

#pragma: preprocessor in C 
#pragma warning(disable: 9876)

7. #inlcude < “filename”>

The #include “filename” preprocessor in C is used to include a header file from a user-specified directory or a current directory inside the system directories. We can provide the directory path inside the #include statement.

#include: preprocessor in C 
Include  “index.css”

Classification of Preprocessor in C 

There are three major types of preprocessors in C. Check out the major classification of preprocessors in C with their implementation below.

1. Macro Statements 

Check out how we can use Macro statements in our C language program.

                               Macro Preprocessor in C 
#include <stdio.h>  

#define PI 3.1415 

main() 

{  

   printf(“The Value of PI is: %f”,PI);  

}  

2. #include < “filename”>

Check out how we can include file elements in our C language program.

                            # Include file Preprocessor in C 
#include <stdio.h>  // This is our file inclusion directive 

#define PI 3.1415 

main() 

{  

 printf(“The Value of PI is: %f”,PI);

3. Conditional Compilation

These preprocessors decide whether to include statements or not. They are generally based on true or false conditions. In which they will include the statements if they satisfy the condition and exclude if they satisfy.

                            #Conditional Statements Preprocessor in C 
#include <stdio.h>

#define Num 19

int main()

{

   #ifdef Num   // conditional compilation directives

   printf (“The number found is %d. \n”, Num);

  #else

  printf(“ The number found is not %d \n”, Num)

   #endif  // also a conditional compilation directive

   printf(“It satisfies the condition.\n”);

   return 0;

}

Start learning C programming with the PW Skills Online Course. Enrol now to build a successful future in programming: C Programming Foundations

Recommended Course

Preprocessor in C FAQs

What is a preprocessor in C?

A block of statements processed before the start of the compilation is called a Preprocessor directive in C. They are not included in compilers.

How many types of preprocessors are in C?

There are generally four types of preprocessor in C, namely define, include, undef, if, ifdef, etc. Check the article above for more details.

What is the work of a preprocessor in C?

Preprocessors in C include certain operations before the compilation process begins. They are used inside our program and are important to be processed before the compilation starts.

What are four different types of Preprocessor in C?

The four different types of preprocessor in C are:
1. Macro

2. File inclusion

3. Include

4. Conditional

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.