X-Marco
http://www.drdobbs.com/cpp/184401387
//------------------ File: color_table.h
X(red, "red")
X(green, "green")
X(blue, "blue")
// ----------------- File: main.c
#include <stdio.h>
#define X(a, b) a,
enum COLOR {
#include "color_table.h"
};
#undef X
#define X(a, b) b,
char *color_name[] = {
#include "color_table.h"
};
#undef X
int main() {
enum COLOR c = red;
printf("c=%s\n", color_name[c]);
return 0;
}
Note: enum and char *[] are combined in a single include file color_table.h
espacilly, C has designed initializer attribute
文章標籤
全站熱搜

#include
//#define ENABLE
#ifdef ENABLE
# define NORMAL(index, function_name) FUNCTION(index, function_name)
# define VENDOR(index, function_name) FUNCTION(index, function_name)
#else
# define NORMAL(index, function_name) FUNCTION(index, function_name)
# define VENDOR(index, function_name) FUNCTION(index, bad)
#endif
void good(int i)
{
printf("\n marco take effect = %d \n ", (i) );
}
void bad()
{
printf("\n marco disable = \n " );
}
int main()
{
int i,j;
i = 5;
j = -9;
#define FUNCTION(index, function_name ) function_name(index)
NORMAL(i, good );
VENDOR(j, good );
#undef FUNCTION
return 0;
}