C macro enables a feature called token pasting which allows us to paste or append an extra token to the existing token and takes the appended tokens as a single token at the time of compilation. In the following example, we have taken a token 'user_id' and appended extra tokens like 1, 2 and 3 to it using ## token pasting operator. Operator # converts the token to a string token.

Code:
int user_id1 = 10, user_id2 = 20, user_id3 = 30;

#define print_userid(n) printf("Variable user_id" #n "=%d\n", user_id ##n);

int main(int argc, char *argv[])
{
  print_userid(1);
  print_userid(2);
  print_userid(3);
  return 0;
}
Output:
Variable user_id1=10
Variable user_id2=20
Variable user_id3=30

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#