Variable name extrapolation

Hi.

Is there an easy way to extrapolate a variable's name and print it?

For example, if I have a variable named count, is there a built-in procedure that will take count, and return the word "count"?

I'm writing some debugging procedures, and having this would be very helpful.

Thanks in advance for the help.
"Variable name extrapolation" doesn't mean anything. I can't help but wonder where you got that term.

No, there's no way to do that, but since the variable names are known at compile time, you can just do std::cout <<"count="<<count.
Last edited on
It was the closest thing I could think of to describe what I wanted, I didn't get it from anywhere. I'm a beginner programmer - I know some basic things, but I'm certainly not proficient, so I apologize if I don't know the language to describe things quite yet.

I'm looking for something that basically does the opposite of "getenv":
http://www.cplusplus.com/reference/clibrary/cstdlib/getenv.html

getenv will find a variable name that you send it, and return it's value. I want to send a variable to something, and have it return a string that is the name.

Any ideas?

getenv() applies to environment variables, which are set and used by the OS for a variety of purposes (e.g. in Windows, CD contains the working directory).

Variable names aren't known at run time. They exist only for the programmer's convenience, and are translated to memory addresses at compile time. So, like I said, what you're asking is impossible (in C/++, anyway).
Last edited on
FWIW the preprocessor has a stringize token. Off the top of my head I cannot remember it; I think it might be ##?

#define stringize( x ) ##x

If you were to type

stringize( foo )

it would return the string "foo".

But

void f( int x ) {
cout << stringize( x ) << " = " << x << endl;
}

int z = 4;
f( z );

will output "x = 4" not "z = 4".
Topic archived. No new replies allowed.