Wednesday 26 December 2012

C/C++ Puzzles: PART - 12

Stringizing Operator ( # )

  Stringizing Operator in C/C++ converts macro parameters to string literals without expanding the parameter definition. This is a Preprocessor operator in C/C++. The example program given below illustrates the working of Stringizing Operator.

#include<stdio.h>
#define String(i) #i
int main()
{
   int tot1=10,tot2=20,tot3=30,i,TotVal;
   TotVal=tot1+tot2+tot3;
   printf("\n\nThe Stringizing Operator is used to print TotVal.\n\n");
   printf("\nThe value of "String(TotVal)" is %d\n\n",TotVal);
}
Token Pasting Operator ( ## ) 

  This is another Preprocessor Operator in C/C++. The token pasting (##) operator eliminates any white space around it and concatenates the non-whitespace characters together. It can only be used in a macro definition. The example program given below illustrates the working of Token Pasting Operator.

#include<stdio.h>
#define join(a,b) a##b
int main()
{
   int tot1=10,tot2=20,tot3=30,i,TotVal;
   TotVal=tot1+tot2+tot3;
   printf("\n\nThe Token Pasting Operator is used to print TotVal.\n\n");
   printf("\nThe value of TotVal is %d\n\n",join(Tot,Val));
}


Download.

No comments:

Post a Comment