Show List

Preprocessor Directives

The C preprocessor is a tool that processes source code before it is compiled. It provides various directives that are used to manipulate source code and control the compilation process.

Some of the commonly used preprocessor directives are:

  • #define: This directive is used to define macros, which are symbols that represent a constant value or a piece of code. Macros are expanded by the preprocessor, so that the actual code is replaced with the value or code associated with the macro.

Example:

c
Copy code
#define PI 3.14
  • #include: This directive is used to include header files in your source code. A header file is a file that contains declarations for functions, variables, and other constructs that are used in your program.

Example:

c
Copy code
#include <stdio.h>
  • #ifdef, #ifndef, and #endif: These directives are used to conditionally include or exclude portions of code based on the existence of a macro.

Example:

c
Copy code
#define DEBUG #ifdef DEBUG printf("Debug mode enabled\n"); #endif
  • #pragma: This directive is used to specify a specific action to be taken by the compiler. For example, you can use the pragma once directive to ensure that a header file is included only once in a program.

Example:

c
Copy code
#pragma once
  • #error: This directive is used to issue an error message during the preprocessing stage. It is often used in combination with conditional directives to ensure that certain conditions are met before compiling the code.

Example:

c
Copy code
#ifndef MAX_SIZE #error MAX_SIZE not defined #endif

The preprocessor directives are processed by the preprocessor before the code is compiled. The preprocessor reads the source code and replaces macros, includes header files, and processes conditionals, producing a modified version of the code that is then compiled by the compiler.


    Leave a Comment


  • captcha text