Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a C macro PRINT(x) which prints x
Here we will see how to define a macro called PRINT(x), and this will print whatever the value of x, passed as an argument.
To solve this problem, we will use the stringize operator (#). Using this operator the x is converted into string, then by calling the printf() function internally, the value of x will be printed.
Syntax
#define PRINT(x) printf(#x)
Example
Let us see the example to get the better idea −
#include <stdio.h>
#define PRINT(x) printf(#x)
int main() {
PRINT(Hello);
printf("<br>");
PRINT(26);
printf("<br>");
PRINT(2.354721);
printf("<br>");
return 0;
}
Hello 26 2.354721
How It Works
The stringize operator (#) converts the macro argument into a string literal. When PRINT(Hello) is called, the preprocessor replaces it with printf("Hello"). This allows printing the literal text representation of whatever is passed to the macro.
Conclusion
The PRINT(x) macro uses the stringize operator to convert macro arguments into string literals. This provides a simple way to print literal text representations of values or identifiers in C programs.
