Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Preprocessor

The ## (token pasting) operator

Reference for the C ## operator: join two tokens into one inside a macro, generate function and variable names, and avoid the expansion-order traps.

The ## operator glues two tokens into a single token during macro expansion. a ## b with a = value_ and b = 1 becomes the identifier value_1. It lets a macro generate names: a family of functions, a struct per type, a counter-based unique variable.

Operands of ## are not macro-expanded before pasting, only after. When you need the value of a macro as part of the new name, add one level of indirection, as with the # operator.

C
#define CONCAT(a, b) a##b
CONCAT(user, _id)      /* user_id */

Try it

C Generating functions for several types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Expansion order and unique names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#define MACRO(a, b) a ## b
#define MACRO(a) prefix_ ## a
Name Description
a ## b Joins the last token of a and the first token of b into one token.

Errors / undefined behavior: the result must be a valid single preprocessing token. Pasting + and - into +- is undefined behavior (it is not one token); compilers report an error. ## cannot be the first or last token of a replacement list.

Usage notes

  • Operands are not expanded first. CONCAT_DIRECT(PREFIX, one) becomes PREFIXone. To paste the expansion, route through a second macro (CONCAT). The second TryIt shows both results.
  • __LINE__ and __COUNTER__ give unique suffixes, but only through the two-step form. __COUNTER__ is a GCC, Clang and MSVC extension, not part of C23.
  • Common uses: per-type function families (vec_push_int, vec_push_double), typedef generation, hidden temporary variable names, token-based tables.
  • Empty arguments: an empty operand makes the result the other operand; this is well-defined.
  • Combine with #: #field inside the same macro is allowed. Use #__VA_ARGS__ for lists.
  • Debugging is harder. Names built by pasting do not appear in the source, so grep and IDE navigation miss them. Keep generated names regular.
  • Alternative: _Generic picks the right function by argument type without needing different spellings at the call site.