The preprocessors are the directives, which give instructions to the compiler to preprocess the information before actual compilation starts. It is one of the unique features of C++. Examples of some preprocessor directives are: #define, #include, #ifndef etc.
Some features of preprocessor directives are-
All preprocessor directives in C++ begin with a '#'.
To continue a directive on the next line, we should place a backslash at the end of the line.
Preprocessor directives do not need to end with a semicolon(;).
We can place the preprocessor directives anywhere in a program, but we usually write them at the beginning of a program. Fibonacci Series in C++
Macros
#define
We use the #define preprocessor directive to define symbolic constants or macros.
Syntax
#define macro_name macro_expansion
Example
#include <iostream>
using namespace std;
#define Msg "Hello Coding Ninjas"
int main()
{
cout<<Msg<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
In the above code, The Preprocessor searches the macro name Msg and substitutes the message "Hello Coding Ninjas".
#undef
The definition of a macro will exist from the #define directive till the end of the program. If you want to undefine this macro, you can use the #undef directive.
Syntax
#undef macro_name
In the #define example, if we give a statement #undef Msg before printing the Msg, then the code will give a compilation error.
File inclusion
#include
File inclusion directive #include is used to include other files in our source program. With the help of file inclusion directive #include, we can include header files that contain definitions of various predefined functions in our program.
Syntax
#include <filename>
Example
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
cout<<"COS(0.5)="<<cos(0.5);
return 0;
}
You can also try this code with Online C++ Compiler
Conditional compilation means the compilation of a piece of code based on a condition. These conditions are checked during the preprocessing phase. Some of the conditional compilation directives are #if, #elif, #endif, #ifdef, #ifndef, #else.
Program to demonstrate the usage of conditional compilation directives in a C++ program.
Code
#include <iostream>
using namespace std;
#define flag 5
#define add(a,b) a+b
int main()
{
int a=45,b=26;
#if flag>4
cout <<"Value of flag is greater than 4"<<endl;
#else
cout<<"Value of flag is less than or equal to 4"<< endl;
#endif
cout<<"Sum of the numbers is "<<add(a,b)<<endl;
cout<<"Program completed"<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
Value of flag is greater than 4
Sum of the numbers is 71
Program completed
In the above program, the flag's value is 5, so the constant expression flag>=4 is evaluated to true, so the statement between #if and #else is compiled.
Other directives
#error
This preprocessor directive is used for debugging purposes.
Syntax
#error error_message
In the above syntax, the argument error_message can contain one or more words with or without quotes.
#pragma
This preprocessor directive specifies implementation-defined instructions to the compiler.
Syntax
#pragma name
In the above syntax, the name is the name of pragma.
#line
This preprocessor directive is used for debugging purposes.
Syntax
#line dec-const string-const
In the above syntax, dec-const is a decimal constant, and string-const is a string constant.
The # and ## Operators
The # and ## operators are two special operators. The # operator is used to convert a text token into a string to be displayed, and The ## operator or token pasting operator is used to concatenate two tokens.
Here is a program to demonstrate # and ## operators.
Code
#include <iostream>
using namespace std;
#define makestr(str) #str
#define concatenate(str1, str2) str1##str2
int main()
{
int ab=456;
cout<<"concatenate(a, b)="<<concatenate(a,b)<<endl;
cout<<"makestr(Coding Ninjas)="<< makestr(Coding Ninjas)<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
In the above code, We define makestr with argument str. It has body #str. When we print this makestr with the help of argument "Coding Ninjas", we see that because of #str, the argument is converted to a string. We have also defined a concatenate function with two arguments, str1 and str2. In the body, we specify str1##str2. Expression str1##str2 means str1str2. When we call concatenate(a, b) in the main function, it actually evaluates ab.
What is the # operator in C++? Ans: The # operator is generally called the stringize operator and turns the argument it precedes into a quoted string.
What is the ## operator in C++? Ans: The ## operator is also known as the token pasting operator. It is used in a macro definition to concatenate two tokens into a single token.
What is include directive in C++? Ans: This type of preprocessor directive tells the compiler to include a file in the source code program.
Key takeaways
In this blog, We have discussed various preprocessor directives provided by C++ along with their examples. This blog is over, but the thirst for learning is not over yet. Continue your journey of acquiring knowledge with our practice platform Coding Ninjas Studio to learn, attempt, read interview experiences, and much more. Till then, have a nice day and Happy Coding!