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

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

 Vote 0

Similar topics related to this section

macro vs typedef, debug builds, debug macros, compiler macro for C++, FILE, LINE, DATE, TIME, compiler marcos, token pasting, characterizing, array, multidimensional arrays,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan