Programmatic way to get variable name in C?

2019-01-07 08:48发布

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 the "variable name", followed by its value.

Is there any programmatic way to know the variable name?

9条回答
贼婆χ
2楼-- · 2019-01-07 09:23

Shorter way:

#define GET_VARIABLE_NAME(Variable) (#Variable)

test:

#include <string>
class MyClass {};


int main(int argc, char* argv[]) {
    int foo = 0;

    std::string var_name1 = GET_VARIABLE_NAME(foo);
     char* var_name2 = GET_VARIABLE_NAME(foo);
     char* var_name3 = GET_VARIABLE_NAME(MyClass);


    return 0;
}
查看更多
做自己的国王
3楼-- · 2019-01-07 09:30

You could try something like this:

#define DUMP(varname) fprintf(stderr, "%s = %x", #varname, varname);

I used to use this header I wrote, when I was new to C, it might contain some useful ideas. For example this would allow you to print a C value and provide the format specifier in one (as well as some additional information):

#define TRACE(fmt, var) \
        (error_at_line(0, 0, __FILE__, __LINE__, "%s : " fmt, #var, var))

If you're using C++, you could use the type of the passed value and output it appropriately. I can provide a much more lucrative example for how to "pretty print" variable values if this is the case.

查看更多
地球回转人心会变
4楼-- · 2019-01-07 09:32

If you need to do this for arbitrary variables then you'll probably need to use a debugger API that the compiler or platform provides (like DbgHelp on Windows).

On some embedded systems I worked on we've needed to be able to display on command the values of certain important variables that are known ahead of time, and to do that all that we need is a simple Name/pointer table:

typedef 
struct vartab {
    char const* name;
    int * var;
} vartab;


vartab varTable[] = {
    { "foo", &foo },
    { "bar", &bar }
};

Then I just used a little routine that searches the table for the variable name in question, and dumps the name and the data pointed to by the pointer. If you need to dump data other than plain ints, you can extend the structure to also hold a printf-style formatter and change the pointer to be a void* and pass that junk to snprintf() or something to format the data.

Sometimes I'll also use a macro that helps build the table (possibly also declaring the variable). But to be honest, I think that that really just makes it more complex to understand (especially for someone new joining the project - they often have a small "WTF?" moment) and doesn't really simplify things much.

查看更多
戒情不戒烟
5楼-- · 2019-01-07 09:33

If your executable is compiled with debugging information, you may be able to get this info. If not, you're probably out of luck. So you're building a debugger? Why? Existing debuggers for c are very mature. Why not use existing tools instead of re-inventing the wheel?

查看更多
Emotional °昔
6楼-- · 2019-01-07 09:35

People often want programs to self-introspect (or "reflect" in current jargon). But most programming languages offer little (e.g., Java) or none (C) capability to reflect on all the details of a program (variable names, function names, types, expression structures, etc.).

There's a way to do this for all languages: step outside the language, and use a tool that is designed to extract that information from the language. A class of tool that can do this is called a program transformation system.

See this SO answer for a discussion of how to "get variable names" and print values using a program transformation system: Trace changes to variables automatically

查看更多
成全新的幸福
7楼-- · 2019-01-07 09:37

Try this.

#define MACRO_VARIABLE_TO_STRING(Variable) ((void) Variable,#Variable)

Code (void) Variable is void conversion which is no-op then there is the comma operator and macro stringify. So the net result is const char* containing variable name with compiler check is variable really exists.

Now you have the name of your variable and you can use printf() to print its value.

查看更多
登录 后发表回答