algo-geeks algo-geeks - 1 year ago 124
C Question

# and ## in macros

#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

int main()
return 0;

Just by looking at the program one "might" expect the output to be, the same for both the printf statements. But on running the program you get it as:

bash$ ./a.out

Why is it so?

Answer Source

Because that is how the preprocessor works.

A single '#' will create a string from the given argument, regardless of what that argument contains, while the double '##' will create a new token by concatenating the arguments.

Try looking at the preprocessed output (for instance with gcc -E) if you want to understand better how the macros are evaluated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download