There are mainly four types of Pre-processor directives in C++ programming language, and they are: -
- Macros
- File Inclusion
- Conditional Compilation
- Other directives
Macros
Macros in the C/C++ programming language are considered to be one of the most intriguing concepts. These consist of statements written in C++ code using the #define directive, where upon calling the assigned variable, the corresponding value is substituted and executed.
C++ code
// Here we are writing down the simple C++ programming language code snippet
// To demonstrate the concept of C/C++ preprocessors concept with their code
// and relevant display output on the compiler
#include <iostream>
// macro definition through a simple C++ code with #define functionality
#define LIMIT 15
// the main driver code functionality starts from here
int main()
{
// this is simple for a loop running from 0 initially to the LIMIT
for (int i = 0; i < LIMIT; i++) {
std::cout << i << "\n";
}
return 0;
}
Output:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Macros with parameters C++ code
// Here we are writing down the simple C++ programming language code snippet
// To demonstrate the concept of C/C++ preprocessors concept with their code
// and relevant display output on the compiler
#include <iostream>
// macro with parameter definition
// through a simple C++ code with #define functionality
#define AREA(l, b) (l * b)
// the main driver code functionality starts from here
int main()
{
int l1 = 310, l2 = 35, area;
area = AREA(l1, l2);
// this is simple output code running whenever the macro with a parameter
// is executed
std::cout << "The area of the object similar to rectangle is: " << area;
return 0;
}
Output:
The area of the object similar to a rectangle is: 10850
2. File Inclusion
This is a commonly encountered C++ preprocessor that we frequently utilize, incorporating various pre-installed libraries crucial for our fundamental programming requirements on a day-to-day basis.
Syntax
#include<file_name>
Example:
#include<iostream>
#include<bits/stdc++.h>
3. Conditional Compilation
The Conditional unique compilation and preserved directives are vital components that enable us to selectively compile and execute specific sections of code, allowing us to bypass compiling certain parts based on specified conditions.
Syntax
#ifdef name_of_your _macro_created
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;
statement 6;
statement 7;
.
.
.
statement n;
#endif
4. Other directives
Apart from the commands in our C++ programming language, there are two relatively obscure and infrequently utilized directives #undef Directive, #pragma startup and #pragma exit: directives. Below is how they are implemented:
C++ code
// Here we are writing down the simple C++ programming language code snippet
// To demonstrate the concept of C/C++ preprocessors concept with their code
// and relevant display output on the compiler
#include <bits/stdc++.h>
using namespace std;
void function_1_is();
void function_2_is();
#pragma startup function_1_is()
#pragma exit function_2_is()
void function_1_is()
{
cout << "Hello coder on javaCppTutorial.com we are inside the 1st function created now\n";
}
void function_2_is()
{
cout << "Hello coder on javaCppTutorial.com we are inside the 2nd function created now\n";
}
// the main driver code functionality starts from here
int main()
{
void function_1_is();
void function_2_is();
cout << "Hello coder on javaCppTutorial.com we are inside the int main() function now\n";
return 0;
}
Output:
/tmp/ Lj D4 TT sh NS.o
Hello, coder on javaCppTutorial.com. We are inside the int primary () function now.
C++ code
// Here we are writing down the simple C++ programming language code snippet
// To demonstrate the concept of C/C++ preprocessors concept with their code
// and relevant display output on the compiler
#include <iostream>
using namespace std;
void function_1_is();
void function_2_is();
void __attribute__((constructor)) function_1_is();
void __attribute__((destructor)) function_2_is();
void function_1_is()
{
printf("hello coder we are currently inside the function_1_is()\n");
}
void function_2_is()
{
printf("hello coder we are currently inside the function_2_is()\n");
}
// the main driver code functionality starts from here
int main()
{
printf("hello coder we are currently inside the int main() function\n");
return 0;
}
Output:
hello coder we are currently inside the function_1_is()
hello coder, we are currently inside the int main() function
hello coder we are currently inside the function_2_is()
Happy Coding!