打印常量名/变量名

目录

为什么要写这篇

用于测试的主函数

运行结果

完整代码


为什么要写这篇

在编程实现状态机的过程中,遇到一个问题,状态机输出最重要的是状态,如何查看状态,直接打印?那么看到的只是状态所代表的数字,我们理解中的状态应该是一个字符串显示。于是问题变成了如何打印常量名或变量名。

如果常量/变量不多的情况下,直接单独写一条关于这个常量或变量的打印语句就好了,变量名直接给定。但如果是多个常量/变量,那这种方式就太低效了,怎么办?根据# ,将常量名/变量名转换成字符串,然后输出就好了,如何具体做?制作一个结构体数组,结构体中有两个元素,变量和字符串指针。

用于测试的主函数

测试方式就是一条打印语句,可以将所有变量/常量 的名字输出出来


int main(void)
{
    uint8_t size = sizeof(comparison_table) / sizeof(comparison_table[0]);
    for(uint8_t i = 0; i < size; i++)
    {
        printf("%s value is %d \n", comparison_table[i].str, comparison_table[i].sta);
    }

    return 0;
}

运行结果

太多就不贴完整的图片了

完整代码


#include <stdio.h>
#include <stdint.h>

#define STRING(s)  #s



/* 六个方位对应六种颜色:上下 左右 前后 分别为 白绿 黄橙 红蓝 */

typedef enum
{
    /* for example: state_white_red indicate UP: white, FRONT: red */
    state_white_red = 0,
    state_white_yellow,
    state_white_blue,
    state_white_orange,

    state_green_red,
    state_green_yellow,
    state_green_blue,
    state_green_orange,

    state_yellow_white,
    state_yellow_green,
    state_yellow_red,
    state_yellow_blue,

    state_orange_white,
    state_orange_green,
    state_orange_red,
    state_orange_blue,

    state_red_white,
    state_red_green,
    state_red_yellow,
    state_red_orange,

    state_blue_white,
    state_blue_green,
    state_blue_yellow,
    state_blue_orange
} State;

typedef struct
{
    State sta;      ///< 状态对应的常量
    char *str;      ///< 状态对应的标识符(字符串)
} ComparisonInfo;

static ComparisonInfo comparison_table[] =
{
    {state_white_red,       STRING(state_white_red)},
    {state_white_yellow,    STRING(state_white_yellow)},
    {state_white_blue,      STRING(state_white_blue)},
    {state_white_orange,    STRING(state_white_orange)},

    {state_green_red,       STRING(state_green_red)},
    {state_green_yellow,    STRING(state_green_yellow)},
    {state_green_blue,      STRING(state_green_blue)},
    {state_green_orange,    STRING(state_green_orange)},

    {state_yellow_white,    STRING(state_yellow_white)},
    {state_yellow_green,    STRING(state_yellow_green)},
    {state_yellow_red,      STRING(state_yellow_red)},
    {state_yellow_blue,     STRING(state_yellow_blue)},

    {state_orange_white,    STRING(state_orange_white)},
    {state_orange_green,    STRING(state_orange_green)},
    {state_orange_red,      STRING(state_orange_red)},
    {state_orange_blue,     STRING(state_orange_blue)},

    {state_red_white,       STRING(state_red_white)},
    {state_red_green,       STRING(state_red_green)},
    {state_red_yellow,      STRING(state_red_yellow)},
    {state_red_orange,      STRING(state_red_orange)},

    {state_blue_white,      STRING(state_blue_white)},
    {state_blue_green,      STRING(state_blue_green)},
    {state_blue_yellow,     STRING(state_blue_yellow)},
    {state_blue_orange,     STRING(state_blue_orange)},
};

int main(void)
{
    uint8_t size = sizeof(comparison_table) / sizeof(comparison_table[0]);
    for(uint8_t i = 0; i < size; i++)
    {
        printf("%s value is %d \n", comparison_table[i].str, comparison_table[i].sta);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/quanquanxiaobu/article/details/113986016