In PHP and Python one can iterate over the local variables and, if there is only once choice where the value matches, you could say that you know what the variable's name is, but this does not always work. Machine code does not have variable names. C compiles to assembly and does not have any native reflection capabilities, so it would not know it's name. (Edit: per Anton's answer the pre-processor can know the variable's name).

Do there exist programming languages where a variable would know it's name?

It gets tricky if you do something like b = a and b does not become a copy of a but a reference to the same place.

EDIT: Why in the world would you want this? I can think of one example: error checking that can survive automatic refactoring. Consider this C# snippet:

private void CheckEnumStr(string paramName, string paramValue)
    if (paramName != "pony" && paramName != "horse")
        string exceptionMessage = String.Format(
            "Unexpected value '{0}' of the parameter named '{1}'.",
        throw new ArgumentException(exceptionMessage);
CheckEnumStr("a", a); // Var 'a' does not know its name - this will not survive naive auto-refactoring

There are other libraries provided by Microsoft and others that allow to check for errors (sorry the names have escaped me). I have seen one library which with the help of closures/lambdas can accomplish error checking that can survive refactoring, but it does not feel idiomatic. This would be one reason why I might want a language where a variable knows its name.

  • None that I am aware of, and it seems like a bad idea... I mean the entire point of a variable is that the name doesn't matter.
    – Telastyn
    Dec 6, 2012 at 0:24
  • 1
    @Telastyn exactly. I can't think of a case where knowing your assigned name would be useful. Dec 6, 2012 at 0:27
  • @davidcowden - I can think of a few cases where it could be useful, mostly in CRUDy apps or logging or in DSLs. But everywhere else it seems like adding a lot of overhead for something that would mostly be abused.
    – Telastyn
    Dec 6, 2012 at 0:32
  • @David Cowden, per my edit above, error-checking code could benefit from such feature.
    – Job
    Dec 6, 2012 at 0:40
  • 1
    Also, for the curmudgeonly-types in this conversation, I recommend "Introduction: Why Would You Want to do That?" edc.tversu.ru/elib/inf/0016.pdf Dec 6, 2012 at 1:31

1 Answer 1


It depends on what you mean by name. If we rephrase the question as "can we extract, from any variable, a unique identifier for that variable" then in C the answer is a definite yes: the address-of operator will give us that. In code:

int x;
// &x now uniquely identifies x (while it is valid).

If we rephrase it as "given an identifier, can we apply an operation to it to get a string containing it", then the answer in C is once again yes: we can make a macro which, applied to the identifier, will stringify it. Once more in code:

#define str(s) str2(s)
#define str2(s) #s

int x;
// str(x) will now be "x"

If we look in the context and C++, and ask "given an lvalue reference, can we get a textual representation with which this reference was first defined", the answer is no. In fact, there need not be such a variable: we may have a reference to const which was initialised with an rvalue.

It would definitely be possible to create a language similar to C++, but where this information can be found. Thus, theoretically, such a language exists. I am unaware of any that support this in practice; the question as phrased above is mostly meaningful in a language where things are indeed declared, and such languages tend to shy away from the runtime overhead that this would require.

Reaction to edit: this depends on how the auto-refactoring tool works. With the macro solution, the call would be CheckEnumStr(STR(a), a);. The tool could decide to treat the macro invocation as a function call and rename the a, or expand the macro and not modify the "a".

  • Anton, could you provide an example of a macro and the address of? I am familiar with the address of, but I think that other readers would benefit from seeing more details in the answer. Thanks.
    – Job
    Dec 6, 2012 at 0:31
  • @Job: Sure, done. Dec 6, 2012 at 0:38
  • Great! One more thing - please take a look at my edit above. How would you define an error-checking function in C that would survive automatic refactoring (renaming of the variable name) if it is at all possible?
    – Job
    Dec 6, 2012 at 0:49
  • @Job: Also done. Dec 6, 2012 at 0:57
  • Yep. Python also allows you to get the actual address in memory. In JavaScript I think just about any type that's passed "by reference" actually uses the memory pointer under the hood and this is what gets compared when you compare two objects, arrays, etc. but you can't actually get at the physical address. Dec 6, 2012 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.