A Preprocessor is system software (a computer program that is designed to run on a computer's hardware and application programs). It performs preprocessing of the High-Level Language(HLL).
The preprocessor doesn't know about the scope rules of C. Preprocessor directives like #define come into effect as soon as they are seen and remain in effect until the end of the file that contains them the program's block structure is irrelevant.
We can place these preprocessor directives anywhere in our program. Examples of some preprocessor directives are: #include, #define, #ifndef etc.
There are 4 main types of preprocessor directives:
1. File Inclusion
2. Macros
3. Conditional Compilation
4. Other directives
1. File Inclusion:
i. Header File or Standard files: These files contains the definition of pre-defined functions like printf(), scanf(), etc. These files must be included for working with these functions. Different functions are declared in different header files.
Eg: #include < stdio.h>
ii. User-defined files: When a program becomes very large, it is good practice to divide it into smaller files and include them whenever needed. These types of files are user-defined files.
Eg: #include “filename”
2. Macros: Macros are a piece of code in a program that is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘#define’ directive is used to define a macro.
Eg: #define LIMIT 5
Note: There is no semi-colon(‘;’) at the end of the macro definition. Macro definitions do not need a semi-colon to end.
3. Conditional Compilation: Conditional Compilation directives are a type of directives that help to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ ifdef ’ and ‘ endif ’.
Eg:
# ifdef macro_name
statement 1;
statement 2;
statement 3;
. . .
statement N;
#endif
If the macro with the name as ‘macro_name’ is defined then the block of statements will execute normally but if it is not defined, the compiler will simply skip this block of statements.
4. Other directives:
#undef is used to destroy a macro that was already created using #define.
#ifdef returns TRUE if the macro is defined and returns FALSE if the macro is not defined.
#ifndef returns TRUE if the specified macro is not defined otherwise returns FALSE.
#if uses the value of specified macro for conditional compilation.
#else is an alternative for #if.
#elif is a #else followed by #if in one statement.
#endif is used to terminate preprocessor conditional macro.
#error is used to print an error message on stderr.
#pragma Directive: This directive is a special purpose directive and is used to turn on or off some features. This type of directives are compiler-specific i.e., they vary from compiler to compiler.
#pragma startup and #pragma exit: These directives help us to specify the functions that are needed to run before program startup( before the control passes to main()) and just before program exit (just before the control returns from main()).
Recommended Questions
Useful Files
Users Joined
A Preprocessor is system software (a computer program that is designed to run on a computer's hardware and application programs). It performs preprocessing of the High-Level Language(HLL).
The preprocessor doesn't know about the scope rules of C. Preprocessor directives like #define come into effect as soon as they are seen and remain in effect until the end of the file that contains them the program's block structure is irrelevant.
We can place these preprocessor directives anywhere in our program. Examples of some preprocessor directives are: #include, #define, #ifndef etc.
There are 4 main types of preprocessor directives:
1. File Inclusion
2. Macros
3. Conditional Compilation
4. Other directives
1. File Inclusion:
i. Header File or Standard files: These files contains the definition of pre-defined functions like printf(), scanf(), etc. These files must be included for working with these functions. Different functions are declared in different header files.
Eg: #include < stdio.h>
ii. User-defined files: When a program becomes very large, it is good practice to divide it into smaller files and include them whenever needed. These types of files are user-defined files.
Eg: #include “filename”
2. Macros: Macros are a piece of code in a program that is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘#define’ directive is used to define a macro.
Eg: #define LIMIT 5
Note: There is no semi-colon(‘;’) at the end of the macro definition. Macro definitions do not need a semi-colon to end.
3. Conditional Compilation: Conditional Compilation directives are a type of directives that help to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ ifdef ’ and ‘ endif ’.
Eg:
# ifdef macro_name
statement 1;
statement 2;
statement 3;
. . .
statement N;
#endif
If the macro with the name as ‘macro_name’ is defined then the block of statements will execute normally but if it is not defined, the compiler will simply skip this block of statements.
4. Other directives:
#undef is used to destroy a macro that was already created using #define.
#ifdef returns TRUE if the macro is defined and returns FALSE if the macro is not defined.
#ifndef returns TRUE if the specified macro is not defined otherwise returns FALSE.
#if uses the value of specified macro for conditional compilation.
#else is an alternative for #if.
#elif is a #else followed by #if in one statement.
#endif is used to terminate preprocessor conditional macro.
#error is used to print an error message on stderr.
#pragma Directive: This directive is a special purpose directive and is used to turn on or off some features. This type of directives are compiler-specific i.e., they vary from compiler to compiler.
#pragma startup and #pragma exit: These directives help us to specify the functions that are needed to run before program startup( before the control passes to main()) and just before program exit (just before the control returns from main()).