Yet Another Oddball: Token Paste

I’m constantly on the prowl for obscure and seldom-used C language functions and techniques. One I just discovered — despite coding in C for decades — is the token paste operator. It’s a weirdo.

Going back to the original K&R (“the manual”), the token paste operator isn’t mentioned. No, you must go to the second edition of K&R, published 10 years later in 1988, to discover the ## preprocessor operator. Even then, the book is vague on specifics or explaining situations where the operator is handy or even necessary.

The token paste operator glues two preprocessor varibles together. Also called the pasting operator, it’s used in a #define directive.

As a review, #define creates a defined constant, a token that can be used throughout the source code file:

#define SIZE 15

The defined constant SIZE is changed to 15 before the code compiles. This constant is available to all functions in the file, whereas a constant integer const int size=15; would be available only within the function where it’s declared. (Also, const int size is a variable and SIZE is replaced before compiling.)

The token paste operator finds its place in those macro constructions that emulate a function. A common example is putchar(), though this macro doesn’t use the token paste operator:

#define putchar(a) putc(a,stdout)

The putchar() “function” is really a macro, defined in the stdio.h header file similar to above, where the putc() function does the actual work. Variable a is lifted from putchar(a) and set into the putc() function as its first argument. This is one way #define directives build macros.

The token paste operator works on the macro’s argument(s). It pastes or glues one argument to another or to a constant value. Here’s an example:

#define paste(a,b) a##b

Given arguments a and b, the paste() macro sticks them together as ab in your code. So paste(p,i) results in pi:

#include <stdio.h>

#define paste( a, b ) a##b

int main()
    float pi = 3.141;



In the printf() statement, paste(p,i) sticks the letters p and i together to form pi. This evaluation becomes float variable pi, which is output when the program runs.

Here’s another, more interesting but still questionably useful example:

#include <stdio.h>

#define s1 "Goodbye, "
#define s2 "cruel world!"

#define output(x) s##x

int main()


Two defined constants, s1 and s1, are created. The output() macro prefixes letter s to its argument x. So output(1) expands to s1 and output(2) becomes s2. The printf() statement outputs the resulting strings:

Goodbye, cruel world!

I confess that these two examples are lifted from other websites where an attempt was made to describe how the token paste operator could perhaps be useful. Trying to find my own, clever demonstration code proved futile. Then again, the ## operator is obscure for a reason.

What would be interesting to me is to unravel how this operator came to be. It wasn’t documented in 1978 when the first K&R book appeared. So what happened between then and 1988 when it appeared in the second edition? Was it borrowed from another programming language? Did something occur in computer history that necessitated the operator? Perhaps we’ll never know.

Leave a Reply