Introduction to C Preprocessor in C Programming Language
- A preprocessor is a program that processes Source Code before it passes through the compiler for compilation.
- There are some preprocessor commands and directives that are used to instruct Preprocessor and mostly placed at the starting of the C source code.
- To include the various instructions to the compiler in the C source code, some directives are used called as Preprocessor Directives.
- Preprocessor expands the scope of the programming environment.
- Preprocessor commands are executed before the compiler compiles the source code.
- These commands will change the original code according to the operating environment and adds the code that will be required by calls to library functions.
There are 2 main uses of preprocessor directives :
- To include external files such as Header files(stdio.h , math.h etc).
- To define macros.
Preprocessor commands begins with a symbol # as directives.
Example: A standard input-output header file is written as: #include <stdio.h>
It is a command which tells the preprocessor to include this external header file as part of the program to be compiled.
- Each preprocessor directive must be on its separate line,
Example: Wrong: #include<stdio.h> #include<math.h> Correct: #include<stdio.h> #include<math.h>
- To maintain the portability of C programming language in different architectures or compilers,the concept of Preprocessor is introduced.
- All the preprocessor processed before the staring of actual compilation and create an intermediate file.
- In the intermediate file all preprocessor statement/commands are replaced by corresponding C code. During the process of compilation that intermediate file is deleted by the compiler automatically.
Advantages of Preprocessor in C Language
- C Preprocessor improves the readability of C program.
- C Preprocessor makes the C program easy to maintain.
- C Preprocessor makes the C program more portable.