The ‘C’ preprocessor is a program which processes the source program before compilation. Or we can say that the preprocessor is a program which processes the source code before it passes through the compiler is known as preprocessor.
It performs task like removing comments, include header files, expand
the macros etc. The preprocessor works on
the source code and generates the expanded
source code. Then the expanded source code
is passed to the compiler.
The
commands of the preprocessor are known as preprocessor directives. Preprocessor
directive begin with a hash symbol (#).It
is placed before the main().Some preprocessor directives are: #include, #define, #undef etc. Preprocessor directives are never
terminated with a semicolon.
When the ‘C’ source program is submitted for compilation, the first phase of compilation is taken over by the preprocessor. The preprocessor goes through the program to perform an operation known as “preprocessing”.
Preprocessor directives can be divided into three categories:
- Macro substitution directive
- File inclusion directive
- Conditional Compilation directive
The commonly used preprocessor
directives and their functions are given in the table below:
Directive |
Function |
#define |
Substitutes
a preprocessor macro. |
#undef |
Undefines a preprocessor
macro |
#include |
Specifies the
file to be included |
#ifdef |
Tests for a macro
definition |
#if |
Tests a compile-time
condition |
#else |
Specifies
alternatives when #if test fails |
#endif |
Specifies the end
of #if |
#ifndef |
Test whether a
macro is not defined |
Macro Substation Directives:
The macro substitution is refers to the text replacement facility that is offered by the C preprocessor. #define directive is used by the preprocessor to perform this task and is also known as Macro definition.
There are two types of macros - one which takes the argument and another which does not take any argument.
The general syntax of using the #define directive is as follows:
#define identifier replacement_text
Where,
identifier:
Macro
name
replacement text: it is known as the macro expansion.
Example: #define MAX 5
- This is preprocessor activity.
- The #define is followed by an identifier (macro name) which is followed by the value to be substituted.
- A macro name is generally written in capital letters.
- If suitable and relevant names are given macros increase the readability.
- If a macro is used in any program and we need to make some changes throughout the program we can just change the macro and the changes will be reflected everywhere in the program.
Programming Example: Simple macro
Example:
#define SUM(a,b)
a+b
Example: Macros with arguments
Output:
File Inclusion Directives:
An external file containing functions or macro definitions can be included as part of a program to avoid rewriting those functions or macros. This can be done by using the #include directive.
The general syntax of this directive is as follows:
#include <filename> OR #include “filename”
The content that is included in the filename will be replaced at the point where the directive is written.
The preprocessor directive #include is an instruction to read in the entire contents of another file at that point. This is generally used to read in header files for library functions. Header files contain details of functions and types used within the library. They must be included before the program can make use of the library functions.
The
preprocessor takes specified file, and pastes its contents into the place where
we put #include before the source is
compiled. For example, when we use printf(),
stdio.h is required so that the
compiler knows what printf() is.
Conditional Compilation Directives:
The C preprocessor provides certain directives that control which part of the code are seen by the compiler and which are not. This process of selectively compiling parts of code is known as conditional compilation.The preprocessor has a conditional statement similar to if else. It can be used to selectively include statements in a program. There are a number of preprocessor directive related to conditional compilation of code.
The keywords for conditional selection are: #ifdef, #ifndef, #else and #endif.
#ifdef: takes a name as an argument, and returns true if the name has already been defined. The name may be defined using a #define.
#ifndef: takes the names as an argument and checks whether it has been defined or not. If the name has not been defined it returns true or else it returns false.
#else: is optional and ends the block beginning with #ifdef It is used to create 2 way optional selection.
#endif: ends the block started by #ifdef or #else. Where the #ifdef is true, statements between it and a following #else or #endif are included in the program. Where it is fakse, and there is a following #else, statements between the #else and the following #endif are included.
0 Comments
if you have any doubts plz let me know...