CPP Preprocessor

Exploring Preprocessors in C++ Programming Language: Paving the Way for Compilation

Preprocessors are instrumental in the early stages of compiling C++ code. They issue instructions to the compiler that influence ho

w the code is preprocessed before actual compilation. All preprocessor directives commence with a #, and only white-space characters can appear before a preprocessor directive on a line. Unlike C++ statements, preprocessor directives lack semicolons. Preprocessor in C++ overview provides a comprehensive understanding of the role and functionality of the preprocessor in the C++ programming language.

You’ve likely encountered the #include directive, which integrates a header file into the source code.

Several preprocessor directives are supported in C++, such as #define, #include, #if, #else, and #line. Let’s delve into the essential ones:

The #define Preprocessor

The #define preprocessor directive is responsible for creating symbolic constants, or macros. Macros are placeholders replaced with specific values before the program compiles. The general format of this directive is:

#define macro-name replacement-text

Following this directive in a file, all future occurrences of the macro in that file will be substituted with the replacement-text prior to compilation. For instance:

#include <iostream>
using namespace std;

#define PI 3.14159

int main () {
   cout << "Value of PI :" << PI << endl;

   return 0;
}

Running the preprocessing on this code, assuming the source code file is present, and then compiling it with the -E option will show that the value is replaced:

$ g++ -E test.cpp > test.p
...
int main () {
   cout << "Value of PI :" << 3.14159 << endl;
   return 0;
}

Function-Like Macros

The #define directive can also define macros that take arguments:

#include <iostream>
using namespace std;

#define MIN(a,b) (((a)<(b)) ? a : b)

int main () {
   int i, j;

   i = 100;
   j = 30;

   cout <<"The minimum is " << MIN(i, j) << endl;

   return 0;
}

Executing this code will output:

The minimum is 30

Conditional Compilation

Preprocessors offer conditional compilation, allowing you to include or exclude portions of code during compilation. This enables you to compile code selectively based on conditions. For example:

#ifndef NULL
   #define NULL 0
#endif

Conditional compilation can also be used for debugging purposes or for toggling features on or off:

#ifdef DEBUG
   cerr << "Variable x = " << x << endl;
#endif

The # and ## Operators

The # operator converts a replacement-text token into a string surrounded by quotes. Meanwhile, the ## operator concatenates two tokens. For example:

#include <iostream>
using namespace std;

#define MKSTR( x ) #x

int main () {
   cout << MKSTR(HELLO C++) << endl;

   return 0;
}

Executing this code will yield:

HELLO C++

Predefined C++ Macros

C++ supplies a set of predefined macros, including __LINE__, __FILE__, __DATE__, and __TIME__, which provide information about the code. For instance:

#include <iostream>
using namespace std;

int main () {
   cout << "Value of __LINE__ : " << __LINE__ << endl;
   cout << "Value of __FILE__ : " << __FILE__ << endl;
   cout << "Value of __DATE__ : " << __DATE__ << endl;
   cout << "Value of __TIME__ : " << __TIME__ << endl;

   return 0;
}

Running this code will output:

Value of __LINE__ : 6
Value of __FILE__ : test.cpp
Value of __DATE__ : Sep 1 2023
Value of __TIME__ : 09:00:00

Preprocessors lay the groundwork for the compiler, instructing it on how to handle code before the actual compilation process starts. Their capabilities extend to defining macros, handling conditional compilation, and providing essential metadata about the code.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading