The # and ## C/C++ usage example in C code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Do a concatenation using the # and ## C/C++ preprocessor directives

To show: The # and ## C/C++ preprocessor directives code sample

 

 

#include <stdio.h>

// define two macros

#define HELLO(x) printf("Hello, " #x "\n");

#define SHOWFUNC(x) Use ## Func ## x

 

int main(void)

{

// new concatenated identifier, UseFuncOne

char * SHOWFUNC(One);

// new concatenated identifier, UseFuncTwo

char * SHOWFUNC(Two);

 

SHOWFUNC(One) = "New name, UseFuncOne";

SHOWFUNC(Two) = "New name, UseFuncTwo";

HELLO(Birch);

 

printf("SHOWFUNC(One) -> %s \n",SHOWFUNC(One));

printf("SHOWFUNC(One) -> %s \n",SHOWFUNC(Two));

return 0;

}

 

Output example:

 

Hello, Birch

SHOWFUNC(One) -> New name, UseFuncOne

SHOWFUNC(One) -> New name, UseFuncTwo

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index