What are Preprocessor Directives in C- Programming

C provides many features like structures, unions and pointers. Another unique feature of the C language is the preprocessor. The C preprocessor provides several tools that are not present in other high-level languages. The programmer can use these tools to make his program easy to read, easy to modify, portable and more efficient.

The preprocessor is a program that processes the source code before it passes through the compiler. Preprocessor directives are placed in the source program before the main line. Before the source code passes through the compiler, it is examined by the preprocessor for any preprocessor directives. If there are any, appropriate actions are taken and then the source program is handed over to the compiler.

All the preprocessor directives follow special syntax rules that are different from the normal C syntax. Every preprocessor directive begins with the symbol # and is followed by the respective preprocessor directive. The preprocessor directives are divided into three categories. They are:

  1. Macro Substitution Directives
  2. File Inclusion Directives
  3. Compiler Control Directives

Macro Substitution Directives

Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens. The preprocessor accomplishes this task under the direction of #define statement. This statement, usually known as a macro definition takes the following form:

 

macro-sub-directive

If this statement is included in the program at the beginning, then the preprocessor replaces every occurrence of the identifier in the source code by the string.

Note: Care should be taken that there is no space between the # and the word define. Also there should be atleast a single space between #defineand the identifier and between the identifier and the string. Also, there will be no semi-colon at the end of the statement.

There are different forms of macro substitution. The most common are:

  1. Simple macro substitution
  2. Argumented macro substitution
  3. Nested macro substitution

Simple Macro Substitution

The simple macro substitutions are generally used for declaring constants in a C program. Some valid examples for simple macro substitution are:

define-examples

Whenever the preprocessor comes across the simple macros, the identifier will be replaced with the corresponding string. For example, in a C program, all the occurrences of PI will be replaced with 3.1412.

Argumented Macro Substitution

The preprocessor allows us to define more complex and more useful form of substitutions. The Argumented macro substitution takes the following form:

arg-macro

Care should be taken that there is no space between the identifier and the left parentheses. The identifiers arg1, arg2, …. , argn are the formal macro arguments that are analogous to the formal arguments in a function definition. In the program, the occurrence of a macro with arguments is known as amacro call. When a macro is called, the preprocessor substitutes the string, replacing the formal parameters with actual parameters.

For example, if the Argumented macro is declared as shown below:

arg-macro-ex

and the macro is called as shown below:

arg-macro-call

Then the preprocessor will expand the above statement as:

arg-macro-call1

File Inclusion Directives

The external files containing functions or macro definitions can be linked with our program so that there is no need to write the functions and macro definitions again. This can be achieved by using the #include directive. The syntax for this directive is as shown below:

include

We can use either of the above statements to link our program with other files. If the filename is included in double quotes, the file is searched in the local directory. If the filename is included in angular brackets, then the file is searched in the standard directories.

Take your time to comment on this article.

Leave a comment