Aug 21, 2016

C Programming #81: Token Concatenation

C Programming #81: Token Concatenation

Token in C are keywords, identifiers (variable name, constant name …), constant (both string and numerical), operators and punctuators. The Basic unit recognized by compiler is token. Token is not further divided by compiler. This article tries to explore token concatenation operator ##


Lets take below example and explore what is token first -

#include <stdio.h>
int main()
{
  int a = 10, b = 20, sum;

  sum = a + b;

  printf("sum is %d\n", sum);

  return 0;
}
sum is 30

Following are the different tokens

  1. keywords - int, return
  2. identifiers - main, a, b, sum, printf
  3. Constants - 10, 20, "sum is %d\n", 0
  4. Operators - =, +
  5. Punctuators - (, ), {, }, ~,~, ;

We could concatenate two tokens using ## operator. Note it is done during the preprocessing.

e.g.

new##variable would create a new token newvariable. This would be done at preprocessing time. And compiler would only see token as newvariable. It is used big time in macros. Say you want to create similar macros.

#define SWAP(type) \
  void swap_#type (type *x, type *y) \
  { \
    type temp; temp = *x; *x = *y; *y = temp;\
  }

Now say you want to create function definition for swap_char, swap_int, swap_float etc. It can be done easily as demonstrated in below program.

#include <stdio.h>
#define SWAP(type) \
  void swap_##type (type *x, type *y) \
  { \
    type temp; temp = *x; *x = *y; *y = temp;\
  }
SWAP(char);
SWAP(int);
SWAP(float);
int main()
{
   char c = 'H', d = 'W'; 
   int a = 10, b = 20;
   float f = 1.1, g = 1.2;

   printf("c and d before swap is %c and %c\n", c, d);
   swap_char(&c, &d);
   printf("c and d after swap is %c and %c\n", c, d);

   printf("a and b before swap is %d and %d\n", a, b);
   swap_int(&a, &b);
   printf("a and b after swap is %d and %d\n", a, b);

   printf("f and g before swap is %f and %f\n", f, g);
   swap_float(&f, &g);
   printf("f and g after swap is %f and %f\n", f, g);

   return 0;
}
c and d before swap is H and W
c and d after swap is W and H
a and b before swap is 10 and 20
a and b after swap is 20 and 10
f and g before swap is 1.100000 and 1.200000
f and g after swap is 1.200000 and 1.100000

Now biggest con is that any programmer would not get the definition of function of swap_char. It is often used with Stringitize operator covered in next article.

No comments :

Post a Comment