Tuesday, June 10, 2008

C++ Preprocessor Operators

1 comment
What are C++ Preprocessor Operators?

Following preprocessor operators are available in C++ that could be used with the #define directive.
  • # is stringify operator turns the operand into a string. Can be used only in macro replacement text. Macro parameter name must follow this operator.
  • ## is concatentation operator to cat preprocessor tokens. Can be used only in macro replacement text. Must not appear in the beginning or end of replacement text.

EXAMPLE: Demonstrate the usage of preprocessor operators
#include <iostream>
using namespace std;

#define myprint1(a) cout << #a << endl
#define myprint2(a,b) cout << a##b << endl

int main()
{
myprint1(hello);
myprint2("hello","world");
return 0;
}

OUTPUT:-
hello
helloworld
If You Enjoyed This, Take 5 Seconds To Share It

1 comment:

  1. The statement below is giving the error:

    "hello and world does not give a valid preprocessing token"

    #define myprint2(a,b) cout << a##b << endl

    ReplyDelete