2

I need some explanation on this code

#define TRACE_PRINT5 (F, X)   fprintf(stderr, "%s" STRGY(__LINE__) ":(" #X "): ", X);

I know what does # on beginning, but I don't understand what does on ":(" #X"): "

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Itra
  • 45
  • 10

2 Answers2

4

This is called "stringizing"; When you add #, the macro will treat the argument like a string (e.g. getting the name of the thing passed in, instead of the value.)

So for example, if you write:

const char* someString = "abc";
TRACE_PRINT5(something, someString); // let's assume this is on line 10 for __LINE__

That should expand to:

const char* someString = "abc";
fprintf(stderr, "%s" "10" ":(" "someString" "): ", "abc");

(Assuming the STRGY macro just converts its argument to a string.)

0x5453
  • 12,753
  • 1
  • 32
  • 61
2

When you put # before something in a macro it gets the value as string literal.

In your case takes X as a string literal. This means that if you pass the variable age with type int it will pass put "age" on the place of #X

Petar Velev
  • 2,305
  • 12
  • 24