Using underscore in variable names

Hi Folks,

My current project is to build a Motorola 6809 emulator.

I started out compiling to a MEGA but just changed to a Due and found a few compiler issues.

Explicitly, the use of a leading underscore character for variable names. This is standard behaviour in naming private variables in a class. I used _PC, _X, _Y, _A, _B etc. The complier complained about _X and _Y and _A. _PC and _B seemed acceptable.

I read that the use of an underscore with a capital letter is a no-no, so I changed the uppercase letters to lower case and aren't getting any more compiler errors.

I wonder why compilation on a MEGA works but not on a Due?

Your variables were in conflict with "core"/library code; different by cup architecture.

_X seems to be defined in the arm ctype.h; it's used to distinguish character classes:

#define	isxdigit(__c)	(__ctype_lookup(__c)&(_X|_N))

I didn't have any problems with _A or _Y, and I don't actually see them in use anywhere; could you post actual failing code?

It is not standard but acceptable. If you used them in a class like you said you wouldn’t have had any conflicts, you must have made them global and they clashed with Arduino libraries

at Qualcomm and elsewhere, underscores only prefixed variables local to a file (i.e. static).

only constant symbol names are Capitalized and variable names are not

camelBack is one approach for symbol names while under_scores might be used within the name

maintain consistency when modifying existing code regardless of its approach

Alas, if some other include file has #define'ed a symbol like _X, that will screw up its use even within a class.
For example, the ctype.h file contains #define _X 0x100
If you then have:

class my3dObjectClass {
  int _X, _Y, _Z;
   :

it will expand to

  int 0x100, _Y, _Z;

which of course is not valid.

As a workaround, you could put #undef _X and etc after your include files (or at the beginning of your sketch.) If you're not using the ctype features, you don't really need those defined. (and if you DID need them, you'll get other error messages!)

At least you will get error, but still, this is incredible

C++ has some reserved names which I think are good to respect.

From the C++11 specification, § 17.6.4.3.2.1

Certain sets of names and function signatures are always reserved to the implementation:

  • Each name that contains a double underscore __ or begins with an underscore followed by an uppercase
    letter (2.12) is reserved to the implementation for any use.
  • Each name that begins with an underscore is reserved to the implementation for use as a name in the
    global namespace.
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.