Pages

Monday, December 31, 2018

Preprocessor Directives


Preprocessor Directives - Overview

It is a program that is invoked by the compiler to process code before compilation.

Once preprocessor directives are applied; changes are applied to the original source code; and the new source code file without directives is created.

Preprocessing is one of the preliminary operation on C and C++ program files before they are passed to the compiler. The preprocessed source program file must be a valid C or C++ program. It allows us to perform the following:
  • Replace tokens in the current file with specified replacement
  • Insert files within the current file
  • Based on given condition- compile sections of the current file
  • Apply machine-specific rules to specified sections of code

A token is a series of characters delimited by white space. The allowed white space characters in as a preprocessor directives are space, horizontal tab, vertical tab, form feed, new-line character and comments.

Some of the preprocessor directives are:
  • #include Inserts text from another source file.
  • #define Defines a macro.
  • #undef Removes a preprocessor macro definition.
  • #error Defines text for a compile-time error message.
  • #if Conditionally suppresses portions of source code
Preprocessor directives begin with the # token and # is followed by a preprocessor keyword. The # is not part of the directive name and can be separated from the name with white spaces.


Example of Preprocessor Directives:



No comments:

Post a Comment