# and ## extension order

The C standard gives an example:

#define hash_hash # ## # #define mkstr(a) # a #define in_between(a) mkstr(a) #define join(c, d) in_between(c hash_hash d) char p[] = join(x, y); // equivalent to char p[] = "x ## y"; 

But he also says: "The evaluation order of the operators # and ## is not specified."

Why is the hash_hash extension guaranteed to be interpreted as the ## operator applied to # instead of the # operator applied to ##?

+7
source share
2 answers

Because "#" only acts as an operator if it appears in a functionally similar macro and is followed by a parameter name ... but hash_hash is not a functionally similar macro, and those "#" are not respected by parameter names.

+3
source

Quote C99:

Punctuation is a symbol that has independent syntactic and semantic meaning. Depending on the context, it can indicate an operation (which, in turn, can give a meaning or designation of a function, produce a side effect or a combination of them), in which case it is known as an operator (other forms of the operator also exist in some contexts). An operand is an object on which an operator acts.

# and ## are punctuation.

Besides:

6.10.3.1 Substitution of the argument

After the arguments for calling a functionally similar macro have been defined, the argument is replaced. The parameter in the substitution list, except for which the token # or ## prectcessing is preceded or followed by the ## pre-processing token (see below) is replaced with the corresponding argument after all the macros contained in it have been expanded. Prior to being replaced, each argument preprocessing token was completely replaced by the macro, as if they formed the rest of the preprocessing file; no other pre-processing tokens.

0
source

All Articles