0% found this document useful (0 votes)
9 views4 pages

C Preprocessor Directives Explained

Preprocessor directives in C, starting with the # symbol, are commands executed before code compilation, allowing for macros and conditional compilation. Macros are defined using #define and can simplify code, improve readability, and enable code reusability. The document also explains the use of #ifdef, #undef, and #endif for managing macro definitions and conditional code compilation.

Uploaded by

karthikapec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

C Preprocessor Directives Explained

Preprocessor directives in C, starting with the # symbol, are commands executed before code compilation, allowing for macros and conditional compilation. Macros are defined using #define and can simplify code, improve readability, and enable code reusability. The document also explains the use of #ifdef, #undef, and #endif for managing macro definitions and conditional code compilation.

Uploaded by

karthikapec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PREPROCESSOR DIRECTIVES

Preprocessor directives are commands that begin with a # symbol in C


programs. They are processed before the actual compilation of the code starts
— that’s why it’s called the preprocessor.

In C programming, a macro is a fragment of code that is given a name. When


the program is compiled, the preprocessor replaces every occurrence of the
macro name with its defined code before compilation.

Macros are defined using the #define directive.

#include <stdio.h>

#define PI 3.1416

#include <stdio.h> // standard input-output functions

#include "myheader.h" // user-defined header file

#define MAX 100

#define SQUARE(x) ((x) * (x))

#include <stdio.h>

#define PRINT_SUM(a, b) \

printf("Sum = %d\n", (a) + (b));

int main() {

PRINT_SUM(3, 7);

return 0;

}
#ifdef — If Defined

#ifdef means “if this macro is already defined”.

The code inside the block will compile only if the macro is defined.

#undef — Undefine a Macro

#undef removes the definition of a macro.

#endif — End of Conditional Block

Every #ifdef, #ifndef, or #if must be closed with #endif.

It tells the preprocessor where the conditional section ends.

Importance Explanation Example


You can include ready-made #include <math.h> lets you use
Code Reusability
functions using header files. sqrt(), pow(), etc.
Readability and Use macros to make code cleaner #define PI 3.14 → change once,
Maintenance and easy to update. apply everywhere.
Conditional Helps in debugging and building Use #ifdef DEBUG for testing
Compilation different versions of code. messages.
Compilation Unnecessary code can be excluded Reduces compile time for large
Efficiency before compiling. projects.
You can write code that adapts to Use macros to define platform-
Portability
different systems. specific features.

#include <stdio.h>

#define DEBUG // define a macro

int main() {

int x = 10, y = 20;


#ifdef DEBUG

printf("Debug Info: x = %d, y = %d\n", x, y);

#endif

printf("Sum = %d\n", x + y);

#undef DEBUG // undefine the macro

#ifdef DEBUG

printf("This line will NOT be compiled now.\n");

#endif

return 0;

#include <stdio.h>

#define MAX 100

#undef MAX
#define MAX 200 // Re-defined with a new value

int main() {

printf("MAX = %d\n", MAX);

return 0;

You might also like