C++ Pre Processors - C++ Programming Tutorial
C++ Course / Miscellaneous / C++ Pre Processors

C++ Pre Processors

BLUF: Mastering C++ Pre Processors is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Pre Processors

C++ is renowned for its efficiency. Learn how C++ Pre Processors enables low-level control and high-performance computing in the tutorial below.

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

Example

// 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:

Output

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Macros with parameters C++ code

Example

// 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:

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

Example

#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

Example

#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

Example

// 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:

Output

/tmp/ Lj D4 TT sh NS.o
Hello, coder on javaCppTutorial.com. We are inside the int primary () function now.

C++ code

Example

// 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:

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!

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience