typestar

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

  1. #x becomes a string literal of the argument's text.
  2. a ## b forms a single new identifier.
  3. A helper layer is needed to expand a macro before stringifying it.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Text manipulation, step 5 of 12 in Preprocessor & headers.

← Previous Next →