Preprocessor Directives
This is a brief explanation of preprocessor directives in C/C++.
Long time no see. Yeah, I know, the procrasination got the better of me. But heyyyy!!! Here I am, back with another dump.
So, coming back to the main topic, preprocessor directives. Tbh, this is not a new topic to me. I have been using them for quite some time now. But GOD, I didn't know the depth of it. While working on a project recently, I came across something that forced me to revisit the topic. And here I am, sharing my knowledge with you.
All the references are taken from here & here.
What are Preprocessor Directives?
Preprocessor directives are lines included in the code of programs with the intention to help the compiler process the code before actual compilation. In simple words, they are commands that are executed before the actual compilation begins, basically telling the compiler to perform certain tasks. They are preceded by a hash symbol (#).
The preprocessor directives are used for various purposes like defining constants, including header files, conditional compilation, etc, all of which are discussed in detail below.
Types of Preprocessor Directives
There are several types of preprocessor directives in C/C++. Some of the most commonly used ones are:
- Define Directive
- Include Directive
- ifdef, ifndef, else, endif Directives
- Error Directive
- Pragma Directive
Let's discuss each of them in detail.
#Define Directive
The #define
directive is to create a macro. When the preprocessor encounters this directive, it replaces all the occurrences of the macro name with the value specified in the directive.
Syntax : #define <macro_name> <value>
It is a good practice to use #undef
directive before defining a macro to avoid any conflicts.
#Include Directive
The #include
directive is used to include the contents of a file at the specified location in the program. It is used to include header files in the program.
It is good practice to add macros and constants in header files and include them in the main program using the #include
directive.
Syntax : #include <filename>
You can change the paths in which the compiler will search for the header files using the -I
flag or the INCLUDE
environment variable.
#Ifdef, #Ifndef, #Else, #Endif Directives
These directives are used for conditional compilation i.e., include or exclude certain parts of the code based on the condition.
#Error Directive
The #error
directive is used to generate a compilation error with the specified error message.
Syntax : #error <message>
#Pragma Directive
This directive is used to specify implementation-specific directives to the compiler i.e., turn on or off certain features of the compiler.
Syntax : #pragma <directive>
The use of #pragma
directive is discouraged as it is compiler-specific and may not work with all compilers.
For Some Other Night
Again, I'm sorry for the long gap. But who I'm feeling sorry to? 😅 Tbh it's not that I'm not working on anything; I'm just not able to write about it. Anyway enough of this rant. Coming up something interesting soon, ever heard of Bigrams? Stay tuned.