What is Preprocessor in C ?
In C, a preprocessor is a part of the C compiler that performs text manipulations before the actual compilation process begins. It is responsible for preprocessing source code before it is sent to the compiler. Preprocessors are activated by special commands, called preprocessor directives, that start with a ‘#’ symbol. Preprocessors are used for tasks such as including header files, conditional compilation, macro expansion, and more. They help in making the code more maintainable and portable.
Preprocessors in C
Here is a table of some commonly used preprocessor directives in C, along with their descriptions:
Sr. No. | Preprocessor Directive | Description |
---|---|---|
1 | #include | Includes the content of a header file in your code. |
2 | #define | Defines a macro for code replacement. |
3 | #undef | Undefines a previously defined macro. |
4 | #ifdef | Checks if a macro is defined. |
5 | #ifndef | Checks if a macro is not defined. |
6 | #if | Conditional compilation based on an expression. |
7 | #else | Alternative branch in a conditional compilation block. |
8 | #elif | Else-if branch in a conditional compilation block. |
9 | #endif | Ends a conditional compilation block. |
10 | #error | Generates a compilation error message. |
11 | #pragma | Provides compiler-specific instructions or options. |
1. #include
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Output:
Hello, World!
Explanation:
The #include
directive is used to include the standard input-output library (<stdio.h>
) in the code. This allows you to use functions like printf
in your program.
2. #define
#include <stdio.h>
#define PI 3.14159265
int main() {
float radius = 5.0;
float area = PI * radius * radius;
printf("Area of the circle: %f\n", area);
return 0;
}
Output:
Area of the circle: 78.539818
Explanation:
#define
is used to create a macro. In this example, we define a macro PI
with a value of 3.14159265
. The macro is then used in the calculation of the area of a circle.
3. #undef
#include <stdio.h>
#define DEBUG
int main() {
#ifdef DEBUG
printf("Debug mode is enabled.\n");
#endif
#undef DEBUG
#ifdef DEBUG
printf("Debug mode is still enabled.\n");
#else
printf("Debug mode is now disabled.\n");
#endif
printf("Program executed.\n");
return 0;
}
Output:
Debug mode is enabled.
Debug mode is now disabled.
Program executed.
Explanation:
#undef
is used to undefine a previously defined macro. In this example, we define the DEBUG
macro initially and check if it’s defined. After #undef DEBUG
, we check again, and it’s no longer defined.
4. #ifdef
#include <stdio.h>
#define DEBUG
int main() {
#ifdef DEBUG
printf("Debug mode is enabled.\n");
#endif
printf("Program executed.\n");
return 0;
}
Output:
Debug mode is enabled.
Program executed.
Explanation:
#ifdef
checks if the macro DEBUG
is defined. In this case, it is defined, so the code within the conditional block is included in the compilation.
5. #ifndef
#include <stdio.h>
#ifndef VERBOSE
#define VERBOSE
#endif
int main() {
#ifdef VERBOSE
printf("Verbose mode is enabled.\n");
#endif
printf("Program executed.\n");
return 0;
}
Output:
Verbose mode is enabled.
Program executed.
Explanation:
#ifndef
checks if the macro VERBOSE
is not defined. In this case, it’s not defined, so the #define VERBOSE
directive defines the macro before the code is compiled.
6. #if
#include <stdio.h>
#define MY_VALUE 42
int main() {
#if MY_VALUE > 50
printf("Value is greater than 50.\n");
#else
printf("Value is not greater than 50.\n");
#endif
return 0;
}
Output:
Value is not greater than 50.
Explanation:
#if
allows conditional compilation based on an expression. In this example, it checks if the macro MY_VALUE
is greater than 50 and executes the appropriate code block.
7. #else
#include <stdio.h>
#define DEBUG
int main() {
#ifdef DEBUG
printf("Debug mode is enabled.\n");
#else
printf("Debug mode is disabled.\n");
#endif
printf("Program executed.\n");
return 0;
}
Output:
Debug mode is enabled.
Program executed.
Explanation:
#else
provides an alternative branch in a conditional compilation block. If the condition specified with #ifdef
is not met, the code within the #else
block is executed.
8. #elif
#include <stdio.h>
#define VERSION 2
int main() {
#if VERSION == 1
printf("Version 1 is selected.\n");
#elif VERSION == 2
printf("Version 2 is selected.\n");
#else
printf("Unknown version is selected.\n");
#endif
return 0;
}
Output:
Version 2 is selected.
Explanation:
#elif
is used for else-if branching within a conditional compilation block. It checks multiple conditions and executes the code block associated with the first true condition.
9. #endif
#include <stdio.h>
#define MY_VALUE 42
int main() {
#if MY_VALUE > 50
printf("Value is greater than 50.\n");
#else
printf("Value is not greater than 50.\n");
#endif
return 0;
}
Output:
Value is not greater than 50.
Explanation:
#endif
marks the end of a conditional compilation block. In this example, it ends the block started with #if
and #else
.
10. #error
#include <stdio.h>
#ifndef CONFIG
#error "CONFIG macro is not defined."
#endif
int main() {
printf("Program executed.\n");
return 0;
}
Output:
error: #error "CONFIG macro is not defined."
Explanation:
The #error
directive generates a compilation error message along with the specified error message when the condition is not met. In this case, it checks if the CONFIG
macro is defined, and if not, it raises a compilation error.
11. #pragma
#include <stdio.h>
#pragma startup myStartupFunction
#pragma exit myExitFunction
void myStartupFunction() {
printf("Startup function called.\n");
}
void myExitFunction() {
printf("Exit function called.\n");
}
int main() {
printf("Main function executed.\n");
return 0;
}
Output:
Startup function called.
Main function executed.
Exit function called.
Explanation:
#pragma
is used to provide compiler-specific instructions or options. In this example, it specifies functions to be called at startup and exit of the program.