Stringify and token paste in C
The # operator turns an argument into a string, ## joins two tokens into one.
#include <stdio.h>
#define STR(x) #x
#define EXPAND_STR(x) STR(x)
#define JOIN(a, b) a##b
#define VERSION 21
void names(void) {
int JOIN(count, er) = 5;
printf("%s\n", STR(VERSION));
printf("%s\n", EXPAND_STR(VERSION));
printf("%d\n", counter);
}
How it works
#xbecomes a string literal of the argument's text.a ## bforms a single new identifier.- A helper layer is needed to expand a macro before stringifying it.
Keywords and builtins used here
intnamesvoid
The run, in numbers
- Lines
- 13
- Characters to type
- 248
- Tokens
- 64
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 38 seconds.
Step 1 of 3 in Text manipulation, step 5 of 12 in Preprocessor & headers.