#
preprocessor operator converts the argument that follows it into a string literal. The preprocessor operator #
can be used only in a function-like macro definition. #include <stdio.h> #define PRINT(name) printf("The value of " #name " is %d\n", name) main() { int abc = 100; PRINT(abc); }Output: The value of abc is 100
Macro Expansion
--> printf("The value of " #name " is %d\n", name)
--> printf("The value of " "abc" " is %d\n", 100)
--> printf("The value of abc is %d\n", 100)
The unary
#
operator produces a string from its operand. Adjacent string literals are getting concatenated in above example. If the operand to #
contains double quotes or escape sequences, they are also expanded.Similarly ## operator concatenate two tokens passed in macro to a single token.
#define JOIN(a,b) a ## b
main()
{
int myname = 100;
printf("%d", JOIN(my,name));
}
It will be converted into printf("%d", myname);
No comments:
Post a Comment