Programming in C: Unit I (d): Preprocessor Directives

Introduction of Preprocessor Directives

Programming in C

The preprocessor is a program that processes the source code before it passes through the compiler.

Unit – I : Basics of C Programming

CHAPTER 4 : PREPROCESSOR DIRECTIVES

Takeaways

• Preprocessor directives

• #error directive

• Pragma directives

• Predefined macro names

• Conditional directives

INTRODUCTION OF PREPROCESSOR DIRECTIVES

The preprocessor is a program that processes the source code before it passes through the compiler. It operates under the control of preprocessor directive which is placed in the source program before the main(). Before the source code is passed through the compiler, it is examined by the preprocessor for any preprocessor directives. In case the program has some preprocessor directives, appropriate actions are taken (depending on the directive) and the source program is handed over to the compiler.

Preprocessor directives are lines included in the c program that are not program statements but directives for the preprocessor. The preprocessor directives are always preceded by a hash sign (#) directive. The preprocessor directive is executed before the actual compilation of program code begins. Therefore, the preprocessor expands all the directives and takes the corresponding action before any code is generated by the program statements.

The preprocessor directives are only one line long because as soon as a newline character is found, the preprocessor directive is considered to end. No semico- lon (;) can be placed at the end of a preprocessor direc- tive. However, the preprocessor directives may contain a comment (which will be simply ignored).

Programming Tip: Preprocessor directives must start with a hash () sign.

Note

In order to extend a preprocessor directive to multiple lines, place a backslash character (\) as the last character of the line. This means that the line is continued in the line following it.

Although the preprocessor directive is usually placed before the main (), practically speaking, it can appear anywhere in the program code. However, if written in between, the directive will be applied only in the remainder of the source file. The advantages of using preprocessor directives in a C program include:

• Program becomes readable and easy to understand.

• Program can be easily modified or updated.

• Program becomes portable as preprocessor directives make it easy to compile the program in different execution environments.

• Due to the aforesaid reasons, the program also becomes more efficient to use.

Programming in C: Unit I (d): Preprocessor Directives : Tag: : Programming in C - Introduction of Preprocessor Directives