What is a Preprocessor in C/C++?

Have you ever thought that how Computer knows the commands that you are writing ? Have you seen that if you type wrong syntax or data types, then it shows error ? Well it’s all due to the preprocessor. For example, the commands like printf, scanf, int, char, float, cout, cin, try, catch, return, >>, <<, ?, if, else etc. All these are defined in header files like #include. The preprocessor programs provide directives which tell compiler to preprocess the source code before compiling. There are 4 main types of preprocessors, these are

  1. Macros
  2. File Inclusion
  3. Conditional Compilation
  4. Other Directives

Macros are defined as a piece of code which is given some name to a variable for later use purpose. Whenever compiler see this, it replaces the code with defined variable and values. It is used by #define directives. For example:  #define value 5.


You can also pass it with arguments like:  #define value(1,a)(2*a)

File Inclusion:  This preprocessor directive tells compiler to include a file in source code program. There are two types of files which can be included by the user:

  1. Header Files: Such as printf(), scanf(), cout, or cin, etc
  • User defined Files: When more and more functionalities are added in program, then it is advice to divide it into smaller files, hence User-defined files can be be included by #include “filename”.
  • Conditional Compilation: These are used for Conditional Statements which are required according to User’s requirement. They mainly used for ifelse, endif, etc.
  • Other Directive: The other directives are not commonly used. The #undef directive is used to make program undefined for the previously defined variables or macro. The other type of directive is #pragma startup and #pragma exit. It helps in specifying the functions that are needed to run before program startup and program exit().

Show More

Leave a Reply

Back to top button