Domanda

I am trying to do the following:

#define mkstr(str) #str
#define cat(x,y) mkstr(x ## y)

int main()
{

   puts(cat(\,n));
   puts(cat(\,t))
   return 0;
}

both of the puts statements cause error. As \n and n both are preprocessor tokens I expected output them correctly in those puts statements, but Bloodshed/DevC++ compiler giving me the following error:

24:1 G:\BIN\cLang\macro2.cpp pasting "\" and "n" does not give a valid preprocessing token

Where is the fact I'm missing?

È stato utile?

Soluzione

The preprocessor uses a tokenizer which will require C-ish input. So even when stringifying you cannot pass random garbage to a macro. ==> Don't make your preprocessor sad - it will eat kittens if you do so too often.

Actually, there is no way to create "\n" via compile-time concatenation since "\\" "n" is a string consisting of the two literals, i.e. "\n".

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top