C Preprocessor

It is a program that processes our source program before it is passed to the compiler. Preprocessor commands (often known as directives) form what can almost be considered a language within C language. We can certainly write C programs without knowing anything about the preprocessor or its facilities. But preprocessor is such a great convenience that virtually all C programmers rely on it. The preprocessor offers several features called preprocessor directives.s. A preprocessor directive, such as #define, has file scope, meaning that defined names are visible from their point of declaration until the end of the source file in which they appear.
The following are preprocessor directives:

  • Macro expansion
  • File inclusion
  • Conditional Compilation
  • Miscellaneous directives

In this program instead of writing 25 in the for loop, we are writing it in the form of UPPER, which has already been defined before main( ) through the statement.

This statement is called ‘macro definition’ or more commonly, just a ‘macro’. What purpose does it serve? During preprocessing, the preprocessor replaces every occurrence of UPPER in the program with 25. Here is another example of macro definition.

File Inclusion

One of the most common uses of the preprocessor is for the inclusion of files into the source text. These are usually header files, but maybe any text file. The file include command is.

Conditional Compilation

The C preprocessor provides a series of directives for conditional compilation: #if, #elif, #else, #ifdef, #ifndef, and #endif. These commands cause the preprocessor to include or exclude sections of the source code from compilation depending on certain conditions. Conditional compilation is used for three main purposes: to optionally include debug code, to enclose non-portable code, and to guard against multiple inclusion of header files.


Miscellaneous directives

#undef is used to undefine a defined macro variable.
#pragma is used to call a function before and after the main function in a C program.