The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your program before it is compiled. These transformations can be the inclusion of header file, macro expansions etc.
All preprocessing directives begin with a # symbol. For example,
#define PI 3.14
Some of the common uses of C preprocessor are:
Including Header Files: #include
The #include preprocessor is used to include header files to C programs. For example,
#include<stdio.h>
Here, stdio.h is a header file. The #include preprocessor directive replaces the above line with the contents of stdio.h header file.
That's the reason why you need to use #include <stdio.h> before you can use functions like scanf() and printf().
You can also create your own header file containing function declaration and include it in your program using this preprocessor directive.
#include “my_header.h”
#define
A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive.
Here's an example.
#define C 299792458 // speed of light
## in #define:
C provides a special concatenation operator ## which can be used in a #define directive as shown below:
#define CONCAT(X, Y) X##Y
The ## operator concatenates the values in X and Y forming a single sequence.
For example, CONCAT (20, 21) will result in 2021.
#ifdef, #else, #endif:
In C, a programmer can instruct the preprocessor whether or not to include a certain section of the code. This is accomplished using conditional directives.
These are similar to if statements in syntax and usage. However, unlike if statements that are executed during runtime, the conditional directives are executed before compilation by the preprocessor.
These directives are used whenever there is a need to compile a portion of the program conditionally. It is also known as conditional compilation.
The most common conditional directive #ifdef (spelled as if-defined) verifies if a given identifier is defined or not.
The syntax for its usage is :
#ifdef identifier or #if defined (identifier)
The source code following #ifdef will be compiled only if the given identifier is defined either in the code or if it is provided as a compilation option as -Didentifier_name.
The opposite of #ifdef is #ifndef, which is written as
#ifndef identifier or #if !defined (identifier)
Consider the following example:
#if defined (MAX)
#define MIN 30
#else
#define MAX 100
#define MIN 200
#endif
Here, MIN 30 will be defined only if the constant MAX is defined earlier than MIN 30. Else, both MAX and MIN will be defined with values 100 and 200 respectively.
#if, #elif, #endif:
Similar to the if-else-if construct, we have a #if-#elif which is used in creating a chain of #if-else statements that can be used to conditionally compile a portion of code.
The general syntax for #if, #elif, #else, #endif is given below:
#if expression1
statement_block1;
#elif expression2
statement_block2;
#elif expression3
statement_block3;
#else
statement_block4;
#endif
#undef:
The #undef directive “undefines” a symbolic constant or a macro identifier that has been defined earlier; i.e., it negates the effects of a #define directive that may have appeared earlier in the program.
The #undef directive is used to remove values defined by #define directives so that they can be redefined with new values.
The format of #undef is
#undef symbolic_name
Given below is an example for #undef:
#include <stdio.h>
#define TEMP 20
int main() {
printf("%d\n", TEMP);
#ifdef TEMP
#undef TEMP
#define TEMP 99
#else
#define TEMP 999
#endif
printf("%d\n", TEMP);
return 0;
}
Output:
20
99
Here, the code first prints 20 and then prints99.