Token Pasting Operator in c, you should know

Token Pasting Operator in c

Token Pasting Operator (##) is sometimes called a merging or combining operator. It is used to merge two valid tokens, it is the reason we called it token concatenation.

In C language, the ## preprocessing operator performs token pasting. At the time of macro expanding two valid taken combined to each other and create a valid token.

Let see the use of token pasting operator in below mention Examples,

In the below code, I am concatenating var with a numeric number. If a newly created token is valid, you will not get any error or warning.

#include <stdio.h>

#define MERGE(token1, token2) token1##token2

int main(void)
{
    int var1 = 10;
    int var2 = 20;
    int var3 = 30;

    printf("%d\n", MERGE(var, 1));

    printf("%d\n", MERGE(var, 2));

    printf("%d\n", MERGE(var, 3));

    return 0;
}

Output: 10 ,20,30

 

In the below code, var4 is not declared. if I compile the code, I will get the error.

#include <stdio.h>

#define MERGE(token1, token2) token1##token2  

int main(void)
{
    int var1 = 10;
    int var2 = 20;
    int var3 = 30;
    
    printf("%d\n", MERGE(var, 1));
        
    printf("%d\n", MERGE(var, 2));

    printf("%d\n", MERGE(var, 3));
    
    printf("%d\n", MERGE(var, 4));

    return 0;
}

Output:

token Pasting operator in c

If you want to learn more about the c language, here 10 Free days C video course for you.

C tutorial

 

If you perform ‘token pasting’ on tokens that do not make a valid token, then you should avoid the token pasting.

For example,
if you concatenate var and + together, then it becomes var+ which has no meaning. So if you perform ‘token pasting’ on var and +, the compiler gives you an error or warning.

#include <stdio.h>

#define MERGE(token1, token2) token1##token2

int main(void)
{
    printf("%d\n", MERGE(var, +));

    return 0;
}

Output:

token Pasting operator

In token passing, it is not necessary both tokens come from the macro body. It can be possible only one token comes from the macro body. When you pass an argument in the macro body then it replaces with a parameter name before executing ‘##‘.

Generally, the token pasting operator is beneficial when one or more than two tokens come from the macro body. In the below example code, you can see how we can use the token pasting operator in different ways.

#include <stdio.h>

#define MERGE_TEST1(token1, token2) token1##token2

#define MERGE_TEST2(token) var##token

#define MERGE_TEST3(token) token##var


#define MERGE_TEST4 var##4


int main(void)
{
    int var1 = 8;
    int var2 = 9;
    int _var = 10;
    int var4 = 11;


    printf("%d\n", MERGE_TEST1(var,1));

    printf("%d\n", MERGE_TEST2(2));

    printf("%d\n", MERGE_TEST3(_));

    printf("%d\n", MERGE_TEST4);

    return 0;
}

Output: 8,9,10,11

 

Some library uses the token pasting operator to create multiple variables at once. This technique saves time. See the below example code, where I am creating 5 variables using a single macro body.

#include <stdio.h>

//macro to create 5 var
#define CREATE_5_VAR(name) name##1, name##2, name##3, name##4, name##5



int main(void)
{
    //create variable
    int CREATE_5_VAR(var);

    //store value in variable
    var1 = 10;
    var2 = 11;
    var3 = 12;
    var4 = 13;
    var5 = 14;


    //print store value
    printf("%d\n",var1);

    printf("%d\n",var2);

    printf("%d\n",var3);

    printf("%d\n",var4);

    printf("%d\n",var5);

    return 0;
}

Output: 10,11,12,13,14

 

Using the token pasting operator, we can also run a program without writing the main function. See the below code,

#include<stdio.h>

#define Test m##a##i##n

#define CREATE_3_VAR(name) name##1, name##2, name##3

int Test(void)
{
    //create variable
    int CREATE_3_VAR(var);

    printf("Enter two number\n");

    //store value
    scanf("%d%d",&var1,&var2);

    //add two number
    var3 = var1 +var2;

    printf("Addition of two number = %d\n",var3);

    return 0;
}

Output:

Enter two number
23
1
Addition of two numbers = 24

 

Note:  A## preprocessing token shall not occur at the beginning or at the end of a replacement list for either form of macro definition.

 

Recommended Post

One comment

  1. The C pre-processor seems nice at first but later you realize it causes more problems then it solves. Avoid it except in the simplest cases. Token pasting can create nightmares when debugging and for context-sensitive IDEs.

    To paraphrase an old joke: “You have a problem. So you use the C pre-processor. Now you have two problems.”

Leave a Reply

Your email address will not be published. Required fields are marked *