Programmatic way to get variable name in C?

前端 未结 10 1802
广开言路
广开言路 2020-11-29 23:08

I am developing a tool to dump data from variables. I need to dump the variable name, and also the values.

My solution: Store variable name as a string, and print th

10条回答
  •  既然无缘
    2020-11-29 23:52

    Despite the very precise answer from @Matt Joiner, I wanna write a full short code exemplifying this just to see how simple it is using #define macro

    #include 
    #define dump(v)printf("%s",#v);
    int main()
        {
        char a;
        dump(a);
        }
    

    output: a

提交回复
热议问题