At the first look, it seems that writing a C macro which prints its argument is child’s play. Following program should work i.e. it should print x
#define PRINT(x) (x) int main() { printf("%s",PRINT(x)); return 0; } |
But it would issue compile error because the data type of x, which is taken as variable by the compiler, is unknown. Now it doesn’t look so obvious. Isn’t it? Guess what, the followings also won’t work
#define PRINT(x) ('x') #define PRINT(x) ("x") |
But if we know one of lesser known traits of C language, writing such a macro is really a child’s play. 🙂 In C, there’s a # directive, also called ‘Stringizing Operator’, which does this magic. Basically # directive converts its argument in a string. Voila! it is so simple to do the rest. So the above program can be modified as below.
#define PRINT(x) (#x) int main() { printf("%s",PRINT(x)); return 0; } |
Now if the input is PRINT(x), it would print x. In fact, if the input is PRINT(geeks), it would print geeks.
You may find the details of this directive from Microsoft portal here.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- The OFFSETOF() macro
- CRASH() macro - interpretation
- C | Macro & Preprocessor | Question 1
- C | Macro & Preprocessor | Question 2
- C | Macro & Preprocessor | Question 14
- C | Macro & Preprocessor | Question 4
- C | Macro & Preprocessor | Question 5
- C | Macro & Preprocessor | Question 6
- C | Macro & Preprocessor | Question 7
- C | Macro & Preprocessor | Question 8
- C | Macro & Preprocessor | Question 9
- C | Macro & Preprocessor | Question 10
- C | Macro & Preprocessor | Question 11
- C | Macro & Preprocessor | Question 12
- C | Macro & Preprocessor | Question 13
- C | Macro & Preprocessor | Question 14
- Print a number 100 times without using loop, recursion and macro expansion in C?
- C | Macro & Preprocessor | Question 15
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Write a one line C function to round floating point numbers

