0

I want to stringify the __COUNTER__ and print it as a string not an integer. Is that possible?

printf("%s\n", #__COUNTER__);

I have tried the following, but it does not work.

hello.c:6:19: error: stray ‘#’ in program
printf("%s\n",#__COUNTER__);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Har
  • 3,727
  • 10
  • 41
  • 75
  • What is the definition of counter? Also, you should not be using `#` to use a macro, only to define it. – Christian Gibbons May 24 '18 at 17:00
  • Counter is a monotonically incrementing integer, which is available in the gcc version which I am using – Har May 24 '18 at 17:01
  • 1
    `printf("%d", __COUNTER__);` I have no idea what it means to "print as a string". – Eugene Sh. May 24 '18 at 17:02
  • I want a string not an integer – Har May 24 '18 at 17:02
  • Oh, I see, it is predefined. You will need a stringify macro – Christian Gibbons May 24 '18 at 17:02
  • 1
    And how integer is different from string when printed? – Eugene Sh. May 24 '18 at 17:02
  • :) when printed it isn't but prior to printing it is. It is a small example used to demonstrate that I want a string not an integer. – Har May 24 '18 at 17:04
  • 1
    then just use `snprintf` with `%d`. I have a feeling that the macro proposed in the answer is not really what you need. Better describe the real problem. – Eugene Sh. May 24 '18 at 17:05
  • I do need a compile time constant, snprintf wouldn't solve my problem. I wish it was that simple :) – Har May 24 '18 at 17:10
  • 1
    Ok, then the answers are what you need. Apparently to concatenate it with something else you will need to add another level of indirection. Always think in terms of preprocessor passes. Think like preprocessor :) – Eugene Sh. May 24 '18 at 17:11
  • [`__COUNTER__`](https://stackoverflow.com/questions/20768379/c-counter-definition/20768388#20768388): *"...a special macro which has been introduced by [Visual Studio](https://en.wikipedia.org/wiki/Visual_Studio) and I think is now supported by [GCC](https://en.wikipedia.org/wiki/GNU_Compiler_Collection) too"* – Peter Mortensen May 06 '23 at 19:28

2 Answers2

5

Yes, but it takes an indirection because you can't use the # operator outside of a macro.

#define STRINGIFY_2(a) #a
#define STRINGIFY(a) STRINGIFY_2(a)

printf("%s\n", STRINGIFY(__COUNTER__));

The double macro is required to expand __COUNTER__, otherwise the result would be "__COUNTER__".

If you don't want to reinvent that wheel, that's exactly what BOOST_PP_STRINGIZE does.

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • So if I understand correctly, the first STRINGIFY(__ COUNTER __) would result in STRINGIFY_2(0) which would then result in "0"? (didn't know that # is only valid in a macro definition) – Har May 24 '18 at 17:06
  • 1
    @Har exactly! - – Quentin May 24 '18 at 17:07
  • Is the double-macro dance really needed for this use case? I thought that was only needed when pasting things together. – Christian Gibbons May 24 '18 at 17:09
  • @ChristianGibbons [yes](http://eel.is/c++draft/cpp.stringize#2), for the same reason, that is neither `##` nor `#` expand their argument(s) before pasting or stringizing them. – Quentin May 24 '18 at 17:12
  • Oh, right. I knew there was something I was missing. – Christian Gibbons May 24 '18 at 17:14
1

You have to stringify it with a preprocessor macro.

#define XSTR(s) STR(s)
#define STR(s) #s
printf("%s", XSTR(counter));
0x400921FB54442D18
  • 725
  • 1
  • 5
  • 18