What is the compiler seeing with this macro? [closed]
Asked Answered
S

4

12

Consider this:

  #define STRINGIFY(A) #A

If I then later write:

 STRINGIFY(hello)

Is the compiler actually seeing this:

 #hello

I think it is that additional hash in front of #A that is confusing me.

Sociopath answered 23/12, 2012 at 13:55 Comment(4)
searching for [C] stringify on SO gives plenty of answersFourflush
possible duplicate of Use the preprocessor to convert code into a stringPlatinize
"closed as not real question"? What's not clear? Maybe it's a duplicate, but the question is really clear: it's in the title.Coligny
I have to agree, the question seems quite clear with no room for ambiguitySociopath
C
11

No, the compiler will put the argument between quotes, resulting in this:

"hello"

However, beware that macro substitution doesn't take place near # and ##, so if you really need to stringify the argument (even if it's another macro) it's better to write two macros, the first one to expand the argument and the other one to add quotes:

#define STRINGIFY(x) STRINGIFY_AUX(x)
#define STRINGIFY_AUX(x) #x

Or more generally, if you're compiler supports variadic macros (introduced in C99):

#define STRINGIFY(...) STRINGIFY_AUX(__VA_ARGS__)
#define STRINGIFY_AUX(...) #__VA_ARGS__

If you really need a function-like macro that paste a # at the beginning of the argument, I think you need it to generate a string (otherwise you'd make an invalid token for the compiler), so you could simple generate two string literals that the compiler will "paste":

#include <stdio.h>

#define STRINGIFY(...) STRINGIFY_AUX(__VA_ARGS__)
#define STRINGIFY_AUX(...) #__VA_ARGS__

int main(void)
{
  puts(STRINGIFY(#) STRINGIFY(hello));
}

The main body will look like this in the preprocessor output:

puts("#" "hello");

I hope this is not an obscure corner of the preprocessor that results in undefined behavior, but it shouldn't be a problem since we're not generating another preprocessor instruction.

Coligny answered 23/12, 2012 at 14:11 Comment(0)
P
17

What the compiler sees is this:

"hello"

The hash is preprocessor-only token.

Single hash stringifies the argument.

#define STRINGIFY(x) #x
STRINGIFY(hello)

gets replaced by

"hello"

Double hash concatenates the tokens:

#define CAT(a, b) a##b
#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
STRINGIFY(CAT(hello,world))

gets replaced by this:

_STRINGIFY(helloworld)

and then by this:

"helloworld"

EDIT: As Pubby pointed out, the example was wrong, the macro replacement doesn't work that way, but now I corrected it.

Pumice answered 23/12, 2012 at 13:56 Comment(4)
Well that's great, can you explain what exactly the hash does to the OP, to make this answer somewhat useful?Carnahan
i agree i am still confused. the second hash is then also getting interpreted by preprocessor, so essentially the preprocessor is doing two interpretations when STRINGIFY is used?Sociopath
@andrewx The hash is only to tell preprocessor "stringify this".Pumice
Temporary -1 as STRINGIFY(CAT(hello,world)) does not expand to "helloworld". The rules are weird for argument expansion, see https://mcmap.net/q/518117/-macro-evaluation-order-duplicateDilettante
C
11

No, the compiler will put the argument between quotes, resulting in this:

"hello"

However, beware that macro substitution doesn't take place near # and ##, so if you really need to stringify the argument (even if it's another macro) it's better to write two macros, the first one to expand the argument and the other one to add quotes:

#define STRINGIFY(x) STRINGIFY_AUX(x)
#define STRINGIFY_AUX(x) #x

Or more generally, if you're compiler supports variadic macros (introduced in C99):

#define STRINGIFY(...) STRINGIFY_AUX(__VA_ARGS__)
#define STRINGIFY_AUX(...) #__VA_ARGS__

If you really need a function-like macro that paste a # at the beginning of the argument, I think you need it to generate a string (otherwise you'd make an invalid token for the compiler), so you could simple generate two string literals that the compiler will "paste":

#include <stdio.h>

#define STRINGIFY(...) STRINGIFY_AUX(__VA_ARGS__)
#define STRINGIFY_AUX(...) #__VA_ARGS__

int main(void)
{
  puts(STRINGIFY(#) STRINGIFY(hello));
}

The main body will look like this in the preprocessor output:

puts("#" "hello");

I hope this is not an obscure corner of the preprocessor that results in undefined behavior, but it shouldn't be a problem since we're not generating another preprocessor instruction.

Coligny answered 23/12, 2012 at 14:11 Comment(0)
E
10

You can test it yourself using the -E (*) flag (with gcc/g++):

test.cpp

#define STRINGIFY(A) #A

int main(int argc, const char *argv[])
{
    STRINGIFY(hello);
    return 0;
}

Output of g++ test.cpp -E

# 1 "test.cpp"
# 1 "<command-line>"
# 1 "test.cpp"


int main(int argc, const char *argv[])
{
    "hello";
    return 0;
}

(*): If you use the -E option, nothing is done except preprocessing. - GCC Options Controlling the Preprocessor

Eatage answered 23/12, 2012 at 14:0 Comment(0)
S
3

Found good, simple explanation of using hash in preprocessor here:

http://www.cplusplus.com/doc/tutorial/preprocessor/

Sociopath answered 23/12, 2012 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.