DEV Community

Olalekan Omotayo
Olalekan Omotayo

Posted on

C Preprocessor Directives.

In C language, the preprocessor directives are used to perform certain operations before the code is compiled. These directives begin with a hash symbol (#). Here are some common preprocessor directives:

  1. #include - This directive is used to include the contents of another file in the current file. Example: #include

2.#define - This directive is used to define a macro.
Example:

define PI 3.14159

  1. #ifdef, #ifndef, #else, #endif - These directives are used for conditional compilation. Example: #ifndef DEBUG printf("Debugging is off\n");

endif

  1. #pragma - This directive is used to provide additional instructions to the compiler. Example: #pragma pack(push, 1)

Here's an example of how preprocessor directives can be used in C code:

include

define PI 3.14159

int main() {
float radius = 5.0;
float area = PI * radius * radius;

printf("The area of the circle is: %f", area);

#ifdef DEBUG
    printf("\nDebugging information:\n");
    printf("The radius is: %f\n", radius);
    printf("The area is: %f\n", area);
#endif

return 0;
Enter fullscreen mode Exit fullscreen mode

}

In this example, we have used the #include directive to include the standard input-output header file. We have also defined a macro PI using the #define directive. Finally, we have used the #ifdef directive to conditionally compile some debugging statements.

Top comments (0)