C语言:运行中获取宏名字的方法
发布时间:2021-12-18 17:47:34 所属栏目:教程 来源:互联网
导读:在调试C语言程序时,有时需要打印宏的名字。可以通过定义宏,宏名字的数组来获得。 例如: #include stdio.h #define MACRO_STR(x) {x, #x} typedef struct _macro_str { int id; char *name; }MACRO_STR_T; typedef enum _color{ RED, GREEN, BLUE, }COLOR;
|
在调试C语言程序时,有时需要打印宏的名字。可以通过定义宏,宏名字的数组来获得。 例如: #include <stdio.h> #define MACRO_STR(x) {x, #x} typedef struct _macro_str { int id; char *name; }MACRO_STR_T; typedef enum _color{ RED, GREEN, BLUE, }COLOR; MACRO_STR_T g_color_str[] ={ MACRO_STR(RED), MACRO_STR(GREEN), MACRO_STR(BLUE), {-1, NULL} }; static const char * get_macro_name(MACRO_STR_T* table, int id) { int i = 0; while(table[i].id != -1 && table[i].name != NULL){ if(table[i].id == id) return table[i].name; i++; } return ""; } static const char * get_color_name(COLOR color) { return get_macro_name(g_color_str, color); } int main() { COLOR color = RED; printf("The name of color %d is '%s'. n", color, get_color_name(color)); return 0; } ![]() (编辑:开发网_开封站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐
热点阅读



浙公网安备 33038102330459号