Skip to main content
The preprocessors are the directives, which give instruction to the compiler to preprocess the information before actual compilation starts.
All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do not end in a semicolon (;).
You already have seen a #include directive in all the examples. This macro is used to include a header file into the source file.
There are number of preprocessor directives supported by C++ like #include, #define, #if, #else, #line, etc. Let us see important directives:

The #define Preprocessor

The #define preprocessor directive creates symbolic constants. The symbolic constant is called a macro and the general form of the directive is:
#define macro-name replacement-text 
When this line appears in a file, all subsequent occurrences of macro in that file will be replaced by replacement-text before the program is compiled. For example:
#include <iostream>
using namespace std;

#define PI 3.14159

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

   return 0;
}
Now, let us do the preprocessing of this code to see the result, assume we have source code file, so let us compile it with -E option and redirect the result to test.p. Now, if you will check test.p, it will have lots of information and at the bottom, you will fine the value replaced as follows:
$gcc -E test.cpp > test.p

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

   return 0;
}

Function-Like Macros

You can use #define to define a macro which will take argument as follows:
#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;
}
If we compile and run above code, this would produce the following result:
The minimum is 30

Conditional Compilation

There are several directives, which can use to compile selectively portions of your program's source code. This process is called conditional compilation.
The conditional preprocessor construct is much like the if selection structure. Consider the following preprocessor code:
#ifndef NULL
   #define NULL 0
#endif
You can compile a program for debugging purpose and can debugging turn on or off using a single macro as follows:
#ifdef DEBUG
   cerr <<"Variable x = " << x << endl;
#endif
causes the cerr statement to be compiled in the program if the symbolic constant DEBUG has been defined before directive #ifdef DEBUG. You can use #if 0 statment to comment out a portion of the program as follows:
#if 0
   code prevented from compiling
#endif
Let us try the following example:
#include <iostream>
using namespace std;
#define DEBUG

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

int main () {
   int i, j;
   i = 100;
   j = 30;
 
   #ifdef DEBUG
      cerr <<"Trace: Inside main function" << endl;
   #endif

   #if 0
      /* This is commented part */
      cout << MKSTR(HELLO C++) << endl;
   #endif

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

   #ifdef DEBUG
      cerr <<"Trace: Coming out of main function" << endl;
   #endif
      return 0;
}
If we compile and run above code, this would produce the following result:
Trace: Inside main function
The minimum is 30
Trace: Coming out of main function

The # and ## Operators

The # and ## preprocessor operators are available in C++ and ANSI/ISO C. The # operator causes a replacement-text token to be converted to a string surrounded by quotes.
Consider the following macro definition:
#include <iostream>
using namespace std;

#define MKSTR( x ) #x

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

   return 0;
}
If we compile and run above code, this would produce the following result:
HELLO C++
Let us see how it worked. It is simple to understand that the C++ preprocessor turns the line:
cout << MKSTR(HELLO C++) << endl;
into the following line:
cout << "HELLO C++" << endl;
The ## operator is used to concatenate two tokens. Here is an example:
#define CONCAT( x, y )  x ## y
When CONCAT appears in the program, its arguments are concatenated and used to replace the macro. For example, CONCAT(HELLO, C++) is replaced by "HELLO C++" in the program as follows.
#include <iostream>
using namespace std;

#define concat(a, b) a ## b
int main() {
   int xy = 100;
   
   cout << concat(x, y);
   return 0;
}
If we compile and run above code, this would produce the following result:
100
Let us see how it worked. It is simple to understand that the C++ preprocessor transforms:
cout << concat(x, y);
into the following line:
cout << xy;

Predefined C++ Macros

C++ provides a number of predefined macros mentioned below:
MacroDescription
__LINE__This contain the current line number of the program when it is being compiled.
__FILE__This contain the current file name of the program when it is being compiled.
__DATE__This contains a string of the form month/day/year that is the date of the translation of the source file into object code.
__TIME__This contains a string of the form hour:minute:second that is the time at which the program was compiled.
Let us see an example for all the above macros:
#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;
}
If we compile and run above code, this would produce the following result:
Value of __LINE__ : 5
Value of __FILE__ : main.cpp
Value of __DATE__ : Oct 21 2016
Value of __TIME__ : 01:01:48

Comments

Popular posts from this blog

In this chapter, we will see how to enable remote desktop application. It is important because this enables us to work remotely on the server. To do this, we have the following two options. For the first option, we have to follow the steps given below. Step 1  − Go to Start → right click “This PC” → Properties. Step 2  − On Left side click “Remote Setting”. Step 3  − Check radio button “Allow Remote connection to this computer” and Check box “Allow connection only from computers running Remote Desktop with Network Level Authentication (recommended)” → click “Select Users”. Step 4  − Click Add. Step 5  − Type user that you want to allow access. In my case, it is administrator → click OK. For the  second option , we need to follow the steps given below. Step 1  − Click on “Server Manage” → Local Server → click on “Enable” or Disable, if it is Disabled.
The table creation command requires: Name of the table Names of fields Definitions for each field Syntax: Here is generic SQL syntax to create a MySQL table: CREATE TABLE table_name ( column_name column_type ); Now, we will create following table in  TUTORIALS  database. tutorials_tbl ( tutorial_id INT NOT NULL AUTO_INCREMENT , tutorial_title VARCHAR ( 100 ) NOT NULL , tutorial_author VARCHAR ( 40 ) NOT NULL , submission_date DATE , PRIMARY KEY ( tutorial_id ) ); Here few items need explanation: Field Attribute  NOT NULL  is being used because we do not want this field to be NULL. So if user will try to create a record with NULL value, then MySQL will raise an error. Field Attribute  AUTO_INCREMENT  tells MySQL to go ahead and add the next available number to the id field. Keyword  PRIMARY KEY  is used to define a column as primary key. You can use multiple columns separated by comma to define...
The Windows Firewall with Advanced Security is a firewall that runs on the Windows Server 2012 and is turned on by default. The Firewall settings within Windows Server 2012 are managed from within the  Windows Firewall Microsoft Management Console . To set Firewall settings perform the following steps − Step 1  − Click on the Server Manager from the task bar → Click the Tools menu and select Windows Firewall with Advanced Security. Step 2  − To see the current configuration settings by selecting  Windows Firewall Properties  from the MMC. This  allows access to modify the settings  for each of the three firewall profiles, which are –  Domain, Private and Public  and IPsec settings. Step 3  − Applying custom rules, which will include the following two steps − Select either  Inbound Rules  or  Outbound Rules  under  Windows Firewall with Advanced Security  on the left side of the management console...