- The 2 operators used in the preprocessor are
- #
- ##
- If the macros are present with in the quoted strings it is not replaced by the #defined macro.
- But if the macroname is preceded by a # then the macro is expanded into a quoted string with the parameter replaced by the actual argument
- Eg:
#include<stdio.h>
#define toprint(expr) printf(#expr "=%d\n",expr)
int main()
{
int x=10,y=5;
toprint(x/y);
return 0;
}
when toprint(x/y);is invoked the macro is expanded in to
printf("x/y" "=%d\n",x/y);
and the strings are concatenated.so the statement becomes
printf("x/y=%d\n",x/y);
The output of the above program is x/y=2 - In the above example instead of #expr if only expr is present like
#define toprint(expr) printf("expr=%d\n",expr) then the output is expr=2
- The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion.
- If a parameter in the repacement text is adjacent to a ##, the parameter is replaced by the actual argument,the ## and surrounding white space are removed,and the result is rescanned
- Example The macro Join concatenates its two arguments
#define Join(front,back) front##back
now Join(hello,100) resuts hello100.
- #
- A preprocessor line of the form # has no effect
- if the line has only #symbol the preprocessor ignores the line
This is really informative.. I never knew about the # and ## operator.
ReplyDeletegreat!
ReplyDelete