3

What does the '#' symbol do after the second define? And isn't the second line enough? Why the first one?

#define MAKESTRING(n) STRING(n)
#define STRING(n) #n
phuclv
  • 37,963
  • 15
  • 156
  • 475
Eziz Durdyyev
  • 1,110
  • 2
  • 16
  • 34

2 Answers2

7

This is stringize operation, it will produce a string literal from macro parameter, e.g. "n". Two lines are required to allow extra expantion of macro parameter, for example:

// prints __LINE__ (not expanded)
std::cout << STRING(__LINE__) << std::endl;
// prints 42 (line number)
std::cout << MAKESTRING(__LINE__) << std::endl;
user7860670
  • 35,849
  • 4
  • 58
  • 84
0

Hash symbol takes macro argument into a c-string. For example

#define MAKESTRING(x) #x
printf(MAKESTRING(text));

will print text

And first line is only alternative name for this macro.

mfkw1
  • 920
  • 6
  • 15