Stringizing and Token Pasting Operators in C programming

Stringizing and Token Pasting Operators in C programming

C language has lots of advantages. In this tutorial we are going to see  Stringizing and Token Pasting Operators in C programming. Most of them don’t aware of this topic. Let’s start. Before that we should know what is token in C Programming.

Stringizing and Token Pasting Operators in C programming

Tokens in C

Every C program is a collection of instructions and every instruction is a collection of some individual units. Every smallest individual unit of a c program is called token. Every instruction in a c program is a collection of tokens. Tokens are used to construct c programs and they are said to the basic building blocks of a c program.


In the above program these are the Tokens —-> main, {, }, (, ), int, a, b, final

So Now we have a idea of Tokens. Let’s continue our topic.

Token Pasting Operator (##) in C

The token pasting operator ## defined by ANSI enables us to combine two tokens within a macro definition to form a single token. In another words, The token pasting (##) operator simply eliminates any white space around it and concatenates (joins together) the non-whitespace characters together. It is used to create new tokens.


  • It can only be used in a macro definition.
  • It may not be the first or last characters in the replacement text.
  • We can only use this method for valid tokens only.

Valid tokens are:

  • identifiers (variable names, function names, etc)
  • keywords (int, while, volatile, etc)
  • literals (strings, numbers, characters, true or false)
  • operators and punctuators (+, -, *, (, etc)

See the below example program, then you will get clear picture.

Example 1


value_1 = 1000

Example 2


value1: 1220
value2: 1230

Example 3


Example to use ## Pre-Processor!
*** My-Device Register ***
Control-Register Addr = 0x40004020
Status-Register Addr = 0x40004030
Interrupt-Register Addr = 0x40004040

These will done at Preprocessing time.

Why We need this Token Pasting Operator?

  • It is used for Logs in our Program

#define dbg_print(fmt,args...) printf("Debug: "fmt ,##args)

  • It is used to reduce repetitive typing.
  • This can be taken further and be applied to even an abstract data type, such as a linked list. I’ve constructed myself a generic linked list.

In this case LINKED_LIST(int) would give you,

Stringizing Operator (#) in C

Sometimes you may need to convert a macro’s argument to a string. Parameters are not replaced inside string constants, but you can use the ‘#’ preprocessing operator instead. When a macro parameter is used with a leading ‘#’, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant. Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringizing.

  • This will convert whatever is passed as parameter y into a string.
  • This will delete Leading and trailing white space.
  • If we pass the macro, it wont expand. It will print the string.
  • \ will convert into \\ and " is convert into \"
  • Some characters cannot be stringized – notably, the comma , because it is used to delimit parameters and the right parenthesis ) because it marks the end of the parameter list.


Consider the below code.

I’m using the print_string macro like below.

  1. print_string(Testing);
  2. print_string( Testing Program );
  3. print_string("Testing" "Program");
  4. print_string(Testing DEF Program); (Here DEF is a macro)

These macro will expand like below.

  1. "Testing"
  2. "Testing Program" (leading and trailing spaces were trimmed and space between words was compressed to a single space character)
  3. "\"Testing\" \"Program\""(the quotes were automatically converted)
  4. "Testing DEF Program"(DEF macro wont expand)

Can we use both Stringize and the token-pasting operators in single MACRO?

Yup, We can. In below example we are going to use both operators in single MACRO.

This example results in the following actual output from the preprocessor:

Both the stringize and the token-pasting operators are used in this example.

4 2 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
November 15, 2017 7:17 AM

Well Explained. Thanks!

Would love your thoughts, please comment.x
%d bloggers like this: