2

Program 1:

#include <stdio.h>
#define foo(x, y) #x #y

int main()
{
  printf("%s\n", foo(k, l)); //prints kl
  return 0;
}

Program 2:

#include <stdio.h>
#define foo(m, n) m ## n

int main()
{
  printf("%s\n", foo(k, l)); //compiler error
}

Why such variation in the output of both programs? What is the exact difference between these two programs?

nickie
  • 5,608
  • 2
  • 23
  • 37
user2492165
  • 93
  • 1
  • 6

1 Answers1

5

# is the "stringizing" operator; it turns its argument into a string literal.

## is the "token-pasting" operator; it joins its two arguments into a single token, not necessarily a string literal.

An example:

#include <stdio.h>

#define foo(m, n) m ## n

int main(void) {
    char *kl = "token pasting";
    printf("%s\n", foo(k, l));
}

which prints:

token pasting
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631