What is the concatenation operator?


The concatenation operator (##) is used to concatenate (combine) two separate strings into one single string. The concatenation operator is often used in C macros, as the following program demonstrates:

#include <stdio.h>
#define SORT(x) sort_function ## x
void main(void);
void main(void)
     char* array;
     int   elements, element_size;
     SORT(3)(array, elements, element_size);

In the preceding example, the SORT macro uses the concatenation operator to combine the strings sort_function and whatever is passed in the parameter x. This means that the line

SORT(3)(array, elements, element_size);

is run through the preprocessor and is translated into the following line:

sort_function3(array, elements, element_size);

As you can see, the concatenation operator can come in handy when you do not know what function to call until runtime. Using the concatenation operator, you can dynamically construct the name of the function you want to call, as was done with the SORT macro.


© 2017