This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

variable name confusion

void tst()
{
  { char  x; _x = 0; } // ... error C202: '_x': undefined identifier
  { char _x;  x = 0; } // No errors! Are "_x" and "x" the same here?
}

Parents
  • void tst()
    {
      { char  x; _x = 0; } // ... error C202: '_x': undefined identifier
      { char _x;  x = 0; } // No errors! Are "_x" and "x" the same here?
    }
    

    Drew Davis wrote:
    For the second error, it seems more likely that C51 doesn't correctly remove 'x' when it goes out of scope.

    This is wrong - the following code will be compiled without error message:

    void tst()
    {
      { char _x;  x = 0; } // No errors! Are "_x" and "x" the same here?
    }
    

    and "_x" and "x" are really the same here.

    Drew Davis wrote:
    Identifiers that begin with an underscore are reserved for the language or implementation and shouldn't be used...
    This is correct and may be a reason of the Keil C51 compiler bug (IMHO). The variable names "x" and "_x" should be different... :-(

Reply
  • void tst()
    {
      { char  x; _x = 0; } // ... error C202: '_x': undefined identifier
      { char _x;  x = 0; } // No errors! Are "_x" and "x" the same here?
    }
    

    Drew Davis wrote:
    For the second error, it seems more likely that C51 doesn't correctly remove 'x' when it goes out of scope.

    This is wrong - the following code will be compiled without error message:

    void tst()
    {
      { char _x;  x = 0; } // No errors! Are "_x" and "x" the same here?
    }
    

    and "_x" and "x" are really the same here.

    Drew Davis wrote:
    Identifiers that begin with an underscore are reserved for the language or implementation and shouldn't be used...
    This is correct and may be a reason of the Keil C51 compiler bug (IMHO). The variable names "x" and "_x" should be different... :-(

Children
  • Drew Davis wrote:
    Identifiers that begin with an underscore are reserved for the language or implementation and shouldn't be used...
    This is correct

    Actually, it's not. Identifiers with a leading underscore are reserved only if either

    a) the second character is also an underscore,
    b) the second character is upper-case, or
    c) the name is at file-scope.

    Neither of the above applies to the case at hand, a block-scoped variable with a lower-case second character.