Unique var name with C/C++ macros

Post by Nico Brailovsky @ 2009-06-25 | Permalink | Leave a comment

So, you're working on some macro magic incantation and you need a unique variable name in you C program? Though it may seem simple at first, using __LINE__ for a variable name, the ## operator (concatenation in the preprocesor) won't let you. There's a secret spell to do it anyway:

// Do magic! Creates a unique name using the line number
#define LINE_NAME( prefix ) JOIN( prefix, LINE )
#define JOIN( symbol1, symbol2 ) _DO_JOIN( symbol1, symbol2 )
#define _DO_JOIN( symbol1, symbol2 ) symbol1##symbol2

Great, now you can keep obscuring your programs even more - have fun!


In reply to this post, Why is a level of indirection needed for this concatenation macro? | CopyQuery commented @ 2013-10-29T19:25:53.000+01:00:

[…] found an interesting little blog post that explains how to generate (semi) unique names in a macro by using the line […]

Original published here.


In reply to this post, How do I concatenate str and integer variables using macros? – program faq commented @ 2017-12-27T16:30:10.000+01:00:

[…] reading this answer to a similar problem, and also attempting this similar concept to create level of indirection, I have found no solution, as neither of them work […]

Original published here.