Preprocessor directives in C are commands that provide instructions to the preprocessor, which is a part of the compilation process that processes source code before it is compiled. These directives are not part of the C language itself but are essential for program configuration and control during compilation.
#
: All preprocessor directives begin with the #
symbol.;
) at the end.Here are some of the most commonly used preprocessor directives in C:
#include
: Used to include header files in your program. This is commonly used to include standard libraries or user-defined header files.
#include <stdio.h> // Include standard I/O library
#include "myheader.h" // Include a user-defined header file
#define
: Used to define macros or constants. It replaces occurrences of a specific identifier with a defined value or expression throughout the code.
#define PI 3.14 // Define a constant
#define SQUARE(x) ((x) * (x)) // Define a macro function
#undef
: Used to undefine a macro that was previously defined with #define
. This is useful for avoiding redefinition errors.
#undef PI // Undefine the PI macro
#ifdef
/ #ifndef
: Used to conditionally include code based on whether a macro is defined (#ifdef
) or not defined (#ifndef
).
#ifdef DEBUG
printf("Debug mode is enabled.\n");
#endif
#if
, #else
, #elif
, #endif
: Used for conditional compilation based on the value of macros or expressions.
#if VERSION == 2
printf("Version 2 is running.\n");
#elif VERSION == 1
printf("Version 1 is running.\n");
#else
printf("Unknown version.\n");
#endif
#line
: Used to change the line number and file name for error reporting. This is less commonly used but can be useful in generated code.
#line 100 "newfile.c" // Changes the line number to 100 and the filename to "newfile.c"
#error
: Used to generate a compilation error with a custom message. This can be helpful for enforcing conditions during compilation.
#error "This code is not supported on this platform."
#pragma
: Used to provide additional information to the compiler. The exact behavior of #pragma
is compiler-specific.
#pragma once // Ensure that a header file is included only once
Here’s a simple example demonstrating some preprocessor directives:
#include <stdio.h>
#define PI 3.14
#define SQUARE(x) ((x) * (x))
int main() {
float radius = 5.0;
// Using the defined constant
float area = PI * SQUARE(radius);
printf("Area of the circle: %.2f\n", area);
// Conditional compilation
#ifdef DEBUG
printf("Debug mode is enabled.\n");
#endif
return 0;
}
Including Header Files: The program includes the standard input/output library using #include <stdio.h>
.
Defining Constants and Macros: The constant PI
and the macro function SQUARE
are defined using #define
.
Using the Defined Constant and Macro: The area of a circle is calculated using the defined constant and macro in the main
function.
Conditional Compilation: The program checks if DEBUG
is defined. If it is, a debug message will be printed.
@aCodeTutorials All Rights Reserved
privacy policy | about