Introduction to the pre-processor in C
- Introduction to pre-processor
- Pre-preprocessor
- Macro substitution
- File inclusion directives
- Conditional compilation
The compilation does not begin with the C Preprocessor; rather, it is an independent procedure. A simple text replacement tool that tells the compiler to do any necessary preparation before compilation is a C preprocessor.
A macro processor called the C preprocessor is automatically used by the C
compiler to make certain alterations before compiling your program. Because it
can define macros—shorthand for more complicated constructions—the program is
termed a macro processor.
There are four independent features
available to you in the C preprocessor:
- The header files are a part of
this. You can replace these declaration files in your software.
- The C preprocessor can replace
actual macros in the program with designed macros with the use of macro
extensions. Any element of C code can have its definition shortened by a macro.
- Conditional collection and
precise preprocessing instructions enable the program components to be included
or excluded based on different scenarios.
- Line management when you use a program
to merge or reorganize source files into an intermediate file that is then
compiled, you can use line control to tell the compiler where the source line
originally came from.
Preprocessor directives
Beginning with the hash symbol (#), all
preprocessor commands go here. For readability, preprocessor directives must
begin with the first nonblank character in the first column. This is a complete
guide to the most important preprocessor commands –
Macro substitution
The name and text that should be changed in a
macro are specified by the #define directive. The preprocessor simply replaces
the macro's name with the replacement text in the line of code where the macro
is created.
Now we will try the C program
- The document contains Directories used to
include user-defined header files in C programs.
- The file-inclusive directory looks for the
header file inside the same directory if the path is left blank.
- The first step in file-inclusive directives is
#include.
- By giving its path, you can add a certain
header file to the current scope.
- Instead of using triangular brackets to
include user-defined header files, we employ "Double Quote".
- It instructs the compiler to incorporate each
named file.
Conditional compilation
Conditional Compilation: We can
choose not to compile code that does not match specific requirements or to only
compile code that does by using conditional compilation directives.
- #ifdef: This is the simplest type of conditional
directive. This type of block is known as a conditional group. The
controlled text will appear in the preprocessor output if the macro name
is defined. The controlled text will include preprocessing directives
inside a conditional. Their implementation is reliant on the conditional's
success. You can stack them in layers, but they have to be fully nested.
Put simply, '#endif' always matches the nearest '#ifdef' (or '#ifndef') or
'#if'. Furthermore, you cannot create and terminate a conditional group in
different files.ss
Syntax:
#ifdef MACRO
controlled text
#endif /* macroname */