41

In a C macro, is it possible to capitalize a pasted-in token? For example, I currently have the following macro:

#define TEST(name, keyword) \
    test_##name:
        TEST_##keyword##_KEYWORD

I would invoke this as follows:

TEST(test1, TEST1)

which would yield the following:

test_test1:
    TEST_TEST1_KEYWORD

Now, instead of having to type the same name twice (once with all lower case characters, and again with all upper case characters), is there any way that I could do either of the following, and either change the token into all uppercase letters or all lowercase letters?

TEST(test1) or TEST(TEST1)

Thanks, Ryan

DuneBug
  • 1,543
  • 4
  • 15
  • 28
  • I've seen very large and old codes repeat many words in lower and upper case to achieve what you want. I'd guess from this that this is not possible. – Nils_M Apr 05 '16 at 12:04

3 Answers3

18

As far as I'm aware, the only operations that can be done on tokens in the C preprocessor (at least ISO/ANSI standard) is to replace, 'stringify' or concatenate them. I'm also unaware of any GCC or MSVC extensions that will let you do what you want.

However, people have been coming up with clever (or oddball) ways to do magical (or horrible) things with macros, so I wouldn't be surprised if someone surprises me.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Thanks, Michael. This is pretty much what I was expecting, but I was hoping that someone could surprise me as well. =) – DuneBug Aug 03 '10 at 20:38
15

You could do something like the following (untested, probably typos...)

#define NORMALIZE(TOK) NORMALIZE_ ## TOK

and then for each of the writings that may occur do

#define NORMALIZE_test1 test1
#define NORMALIZE_TEST1 test1

then use the NORMALIZE macro inside your real macro something like

#define TEST(name, keyword)                    \
    test_ ## NORMALIZE(name):                  \
        TEST_ ## NORMALIZE(keyword) ##_KEYWORD

(but maybe you'd have to do some intermediate helper macros until you get all concatenations right)

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 1
    Ha! Ha! You did it the wrong way... he wants `TEST(name)` so you would have `#define NORMALIZE_TEST1_UPPER TEST1` and then `#define NORMALIZE_TEST1_LOWER test1` then you use TEST with the uppercase version: `TEST(TEST1)`. (you could of course do the same with the lowercase, depends how you want to call your `TEST()` macro.) – Alexis Wilke Aug 17 '14 at 03:28
5

It's not possible because the preprocessor works on an input stream of pp-token and has no construct that allows you to decompose these in a meaningful manner.

What the preprocessor has is constructs to replace pp-tokens with macro expansions, concatenate them, remove them (entirely) etc.

This means that your only hope for uppercasing is to start with individual characters and uppercase these and then glue everything together. Uppercasing individual characters is quite straight forward as you only have a finite set to work with. Glueing them together on the other hand would be possible, at least if you limit yourself to a fixed maximal length. You would end up in a macro that would be used like this:

TEST(t,e,s,t,1)
skyking
  • 13,817
  • 1
  • 35
  • 57