Home >Backend Development >C++ >Write a C macro PRINT(x) which prints x

Write a C macro PRINT(x) which prints x

王林
王林forward
2023-09-19 13:25:021422browse

Write a C macro PRINT(x) which prints x

Here we will see how to define a macro called PRINT(x), which will print any value of x passed as argument.

To solve this problem, we will use the stringize operator. Use this operator to convert x to a string, and then by calling the printf() function internally, the value of x will be printed. Let's look at an example to get a better idea.

Example

#include <stdio.h>
#define PRINT(x) printf(#x)
int main () {
   PRINT(Hello);
   printf("</p><p>");
   PRINT(26);
   printf("</p><p>");
   PRINT(2.354721);
   printf("</p><p>");
}

Output

Hello
26
2.354721

The above is the detailed content of Write a C macro PRINT(x) which prints x. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete