Predefined Macro Names

The # and ## Preprocessor Operators

C/C++ provides two preprocessor operators: # and ##. These operators are used in a #define macro.

The # operator causes the argument it precedes to be turned into a quoted string. For example, consider this program:

#include <iostream> using namespace std; #define mkstr(s)  # s int main() {   cout << mkstr(I like C++);   return 0; }

The preprocessor turns the line

cout << mkstr(I like C++);

into

cout << "I like C++";

The ## operator is used to concatenate two tokens. For example, in the following program,

#include <iostream> using namespace std; #define concat(a, b)  a ## b int main() {   int xy = 10;   cout << concat(x, y);   return 0; }

the preprocessor transforms

cout << concat(x, y);

into

cout << xy;

If these operators seem strange to you, keep in mind that they are not needed or used in most programs. They exist primarily to allow some special cases to be handled by the preprocessor.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net