adsense


Friday, 27 November 2015

C++ LANGUAGE THE # AND ## OPERATORS

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



C++ LANGUAGE THE # AND ## OPERATORS




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; 

Above line will be turned 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); 

Above line will be transformed into the following line: 

  cout << xy; 


No comments: