Types of Preprocessor Directives in C Language

Preprocessor comprises two distinct words – Pre and processor. Pre means before, and the processor is something that processes a given data. So combining both the words together, we get preprocessors, which is a program that is processing the source code before its compilation.

Before the program gets compiled, interpreted, and executed. It is checked by the preprocessor. The preprocessors are programs that provide some preprocessor directives in C. These directives tell the compiler to preprocess the given source code before its compilation.

How to Write Preprocessor Directives in C?

Always use # (hash) in front of the Preprocessor directives. The most widely used preprocessor directive is #include, which is followed by the name of the header file. It instructs the compiler to include the header file.

See Also: Storage Classes in C Language

How Does a C Preprocessor Work?

Preprocessing is the first step in compiling a program. A preprocessor does not have a scope of its own. But it comes into usage as soon as it is noticed by the compiler, and remains that way until the end of the program. Various types of preprocessors perform different functions.

  1. Removing of comments

The preprocessor directives remove comments from the program. Comments are for humans to understand certain things and have no other significance in a program.

  1. Including certain files

Certain important files that contain a useful piece of code are already present in C library. To bring them into use, preprocessor directive #include has to be used. Examples are:

#include<stdio.h>

#include<string.h>

#include<math.h>

  1. Used for macro expansion

Macros are nothing but small functions that are easier to process. Macros are generally used during recursion in C Programming. The definition of macros is define using preprocessors.

EXAMPLE OF MACRO

#include <stdio.h>

#define LIMIT 3

int main()

{

for (int i=0; i<LIMIT; i++) {

printf(“Value of i is: %d\n”,i);

}

return 0;

}

Output

Value of i is: 0

Value of i is: 1

Value of i is: 2

Types of Preprocessor Directives in C Language

There are various other types of directives in C. These are,

  1. #undef: When you define a macro, it can be undefined using the #undef preprocessor. That is the only task performed by it. EXAMPLE: #undef LIMIT
  2. #pragma: The #pragma preprocessor directives enable or disable various features. #pragma directive varies from compiler to compiler. Let us take a simple example to understand the #pragma directive.

#include <stdio.h>

void f1();

void f2();

#pragma startup f1

#pragma exit f2

void f1()

{

printf(“This is the first function f1.\n”);

}

void f2()

{

printf(“This is the second function f2.\n”);

}

int main()

{

void f1();

void f2();

printf(“This is the main funciton.\n”);

return 0;

}

Output

This is the main function.

3. Pragma in C

These (pragma startup and pragma exit) are two such pragma directives that are run before the start and finish of a program.

The #pragma warn directive to hide some warning messages that are shown during program compilation.

4. Some Points

In C, there are some more directives like the #endif directive which closes off other directives like #if, #ifdef, and #ifndef.

The #if, #idef and #endif directive is for conditional compilation. This is done when the preprocessor evaluates the expression provided by these directives.

Some Interesting Preprocessor Directive Facts in C

  1. Whenever we include a header file, its contents are copied to the main program file.
  2. Just like functions, macros can also take arguments. But the data type in those arguments is not checked.

#define incre(a) ++a

#include <stdio.h>

int main()

{

char *ptra = “HELLO!”;

int a=07;

printf(“%s\n”, incre(ptra));

printf(“%d\n”, incre(a));

return 0;

}

Output

ELLO!

8

Frequently Asked Questions (FAQs)

Q2) Why #define is used in C?

#define in C defines macros in the source code. Macros declare constant values that you can use throughout the program.

Q5) How typedef and #define differ from each other?

Typedef can give names to only types, but #define can be used to define values too. The best example for this is: using #define, 3.14 can be defined as PI. Also, while the typedef is performed by the compiler, #define is performed by the preprocessor.

Q6) What are the advantages of the preprocessor directives in C?

Some advantages of preprocessor are:

  1. Increases the readability of the program.
  2. It makes the program easier to modify.
  3. The code can be transferrable between two different machines.
  4. The preprocessor also helps in carrying out things like File Inclusion, Conditional Compilation, and Substitution.

Q7) What is Macro in C?

Macro can be defined as a piece of code to which a name has been assigned. So whenever that name is used in the code, it is replaced by the contents of that macro.

Q8) What is #include in C?

This directive in C instructs the preprocessor to include the code/content written in another file into the source code.

Also, Read

 Reference

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest Articles