What is Preprocessor in C Language?

A preprocessor is a processor that allows you to define long-structured abbreviations that you can use in your program in place of a large number of lines of code to a small number of lines of code. In C, the preprocessor is not part of the compiler and is used to translate code before compilation. In C, the preprocessor provides some commands that start with # (pound sign). These preprocessor directives contain a series of declarations as a single macro that is used at the beginning of the program, so they can be used multiple times throughout the program.

How does Preprocessor in C works?

In the C programming language, preprocessor directives are defined by the #hash symbol. Usually when you create a C program it is saved using .c and the preprocessor processes that file, this extension is compiled and linked to the linker that links and produces these object files. An executable file that contains an object file. exe file with .obj.

Types of Preprocessor in C Language:

There are different types of preprocessor directive in C programming Language:

1. Macros:

A macro is a piece of code that contains a set of statements that perform a particular or logical task that you need to use many times in your program. You can declare this predefined macro programmatically whenever you need to. Program logic. This is done by the compiler every time it encounters this macro name in the program, and the compiler replaces this macro name with the set of code defined at the beginning of the program. This is done by defining the name of the macro using the #define directive.

For example:
Here Code:
#include <stdio.h>
#define  MAX 8
int main()
{
printf("To print the numbers using macro definition:\n");
for (int i = 0; i < MAX; i++)
{
printf("%d \n",i);
}
return 0;
}

2. File Inclusion:

This type of preprocessor directive tells the compiler to include the file in the source code program. There are two types of files that users can include in their programs:

Header File or Standard files:

These files contain definitions of predefined functions such as printf () and scanf (). You must include these files to work with these functions. For example, the standard I / O functions are in the “iostream” file, while the functions that perform string operations are in the “string” file.

Syntax:

#include< file_name >

The parentheses “<” and “>” tell the compiler to look for the file in the standard directory.

user defined files:

If your program grows very large, it is a good idea to divide it into smaller files and include them as needed. These types of files are user-defined files. These files can be included as follows:

#include “filename”

3. Conditional Compilation:

Conditional compilation directives are a type of directive that helps you compile a particular part of your program or skip compiling a particular part of your program based on certain conditions. This can be done using two preprocessing commands, “ifdef” and “endif”.

Syntax:

#ifdef macro_name

    statement1;

    statement2;

    statement3;

    .

    .

    .

    statementN;

#endif

If a macro is defined with the name “macroname”, the statement block will execute correctly, but if it is not defined, the compiler will skip the statement block.

4. Other directives:

Apart from the above directives, there are two commonly used directives. these are:

#undef Directive:

The #undef directive is used to override the definition of an existing macro. This directive works as follows:

#undef LIMIT

Use this declaration to override the definition of an existing LIMIT macro. After this declaration, all “#ifdefLIMIT” declarations evaluate to false.

List of preprocessor directives:

1. #include:

The #include preprocessor directive is used to paste the code from the specified file into the current file. Used to include user-defined and system-defined header files. If the include file cannot be found, the compiler generates an error. There are three variations.

  • #include <file>

This variant is used for system header files. Finds the file named file in the list of specified directories and then in the standard list of system directories.

  • #include “file”

This variant is used for the header file of your own program. First look in the current directory for a file called file, then look in the same directory used for the system header files.

  • #include anything else

This variant is called #include calculated. The #include directive whose arguments do not fit the two formats above is a calculated inclusion.

2. Macro’s (#define):

Let’s start with a macro. As described, macros are code segments that are replaced by macro values. Macros are defined with the #define directive.

Syntax:

#define token value 

Types of Macros:

There are two types of macros:

a. Object-like Macros:

An object-like macro is an identifier that is replaced by a value. Widely used to represent numerical constants.

For example:

#define PI 3.1415 

Where PI is the name of the macro that will be replaced by the value 3.14. Let’s look at an example of a macro similar to an object:

Here Code:
#include<stdio.h>  
#define PI 3.1415
main()
{ 
     printf("%f",PI); 
}

b. Function-like Macros:

Function macros look like function calls.

For example:

#define MIN(a,b) ((a)<(b)?(a):(b))   

Where MIN is the name of the macro. Let’s look at an example of a macro as a function:

Here Code:
#include<stdio.h>  
#define MIN(a,b) ((a)<(b)?(a):(b)) 
int main() { 
printf("Minimum between 10 and 20 is: %d\n", MIN(10,20));
}   

3. #undef:

Not defining a macro means canceling its definition. This is done using the # undef directive.

Syntax:

#undef token 

define and undefine Example:
Here Code:
#include<stdio.h> 
#define PI 3.1415 
#undef PI 
main() { 
            printf("%f",PI); 
}

4. #ifdef:

The #ifdef preprocessor directive checks if a macro is defined by #define. If so, run the code.

Syntax:

#ifdef MACRO 

//code 

#endif

5. #ifndef:

The #ifndef preprocessor directive checks if a macro is defined by #define. If so, run the code.

Syntax:

#ifndef MACRO 

//code 

#endif 

6. #if:

The #if preprocessor directive evaluates an expression or condition.

Syntax:

#if expression 

//code 

#endif 

7. #else:

The #else preprocessor directive evaluates an expression or condition if the #if condition is false. It can be used with the #if, #elif, #ifdef, and #ifndef directives.

Syntax:

#if expression 

//if code 

#else 

//else code 

#endif

Syntax with #elif:

#if expression 

//if code 

#elif expression 

//elif code 

#else 

//else code 

#endif

For example:
Here Code:
#include <stdio.h> 
#include <conio.h> 
#define NUMBER 1 
int main() { 
       #if NUMBER==0 
       printf("Value of Number is: %d",NUMBER); 
       #else 
       printf("Value of Number is non-zero"); 
       #endif  
       getch(); 
    }

8. #error:

The preprocessor directive #error indicates an error. If the #error directive is encountered, the compiler will throw a fatal error and skip other compilation processes.

For example:
Here Code:
#include<stdio.h> 
#ifndef __MATH_H 
#error First include then compile 
#else 
void main(){ 
      float a; 
      a=sqrt(7);
      printf("%f",a); 
      }
     #endif

9. #pragma:

The preprocessor directive #pragma is used to provide additional information to the compiler. The compiler uses the #pragma directive to provide machine or operating system functionality. Different compilers use the #pragma directive differently.

Syntax:

#pragma token

For example:
Here Code:
#include<stdio.h> 
#include<conio.h> 
void func() ; 
#pragma startup func 
#pragma exit func 
int main(){ 
printf("\nI am in main"); 
getch(); 
}
void func(){ 
printf("\nI am in func"); 
getch(); 
}

Conclusion:

The C programming language preprocessor concludes that a macro is just a small piece of code that is used as a single name defined at the beginning of a program, and that the macro can be used multiple times throughout the program. Whenever you need a macro value to use, simply specify the name of the macro programmatically.

References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 67 times, 1 visits today)

0 0 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
You cannot copy content of this page
Scroll Up
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.