- Strict composition rules based on alphanumeric characters and the use of the underscore.
- Fundamental distinction between uppercase and lowercase letters and prohibition of the use of reserved words.
- Special restrictions and conventions regarding the use of initial underlines to avoid conflicts with the system.
- Advanced support for Unicode characters and features depending on the compiler used.

When you delve into the world of C++ programming, one of the first things you need to grasp is how to name things. Identifiers are simply the labels we assign to elements of our code, such as variables or functions, so that both the compiler and we know what we're talking about at any given time.
It's not just about typing any random word; there's a set of lexical rules you must follow strictly. If you skip any, the compiler will throw an error and your program won't get past the compilation stage, so understanding these concepts is crucial to avoid banging your head against the keyboard.
What exactly is an identifier and what is it used for?
Basically, an identifier is a string of characters that we use to denote various entities within the program. We can use these names to name objects, variables, classes, structures, unions, or enumerated types. They are also essential for naming class members, functions (whether independent or members), aliases created with typedef, jump labels, macros, and their parameters.
Golden rules for creating valid names
For a name to be accepted by C++, it must follow a specific structure. An identifier can consist of letters, numbers, and the underscore symbol. However, there is a crucial detail: the first character can never be a digit ; it must be a letter or an underscore.
As for the allowed characters, we have the English alphabet in both uppercase and lowercase letters and the symbol _ (underscore)It is very important to remember that C++ is a language case sensitive (case-sensitive). This means that if you define a variable called interes and another call InteresThe program will treat them as two completely different things, with no relation to each other.
Restrictions and forbidden words
There are certain words that come "by default" in the language and have a special meaning for the compiler; these are the reserved or keyword wordsTypical examples are int, class, if, while o returnIt is strictly forbidden to use these words as identifiers on their own, although you could include them within a longer name, such as mi_int_variablewhich is perfectly valid.
Also, be careful with special characters. You can't use spaces, commas, periods, accents, or the letter ñ. If you try to put tamaño_variable If something happens, the compiler will most likely go completely crazy and I flagged an error for you immediately.
Conventions and dangers with underlining
Although the language allows you to start a name with an underscore, you have to tread carefully. The use of two underlines in a row (__A hyphen () or an underscore followed by a capital letter is reserved for language implementations and standard libraries. Using them could cause a name collision and result in a undefined behavior very difficult to debug.
Even starting a name with an underscore followed by a lowercase letter in the global scope is not recommended. Ideally, you should avoid leading underscores altogether, as this is a common practice among compiler developers and could interfere with their internal processes.
Unicode support and MSVC features
In modern versions, C++ allows the use of Unicode characters in identifiers, although support may vary depending on the compiler (as is the case with some versions of GCC). This opens the door to using characters from other languages, provided they comply with normalization rules and are not control characters.
If you use the Microsoft compiler (MSVC), there are a few extra quirks. For example, the dollar sign ($) is accepted as a valid character. Additionally, MSVC has a limit of 2048 significant characters for names, including the type information that the compiler adds internally to handle user-defined types.
Types of identifiers according to their scope
Not all identifiers are used in the same way. We have the unqualified identifiersThese are the simple names we normally use. On the other hand, there are the qualified identifiers, which are those that are preceded by the scope resolution operator ::This tells the compiler exactly where to look for the name, for example, indicating that a function belongs to the namespace std through std::cout.
Good nomenclature practices
To prevent your code from becoming a mess and other programmers from wanting to throw their monitors at your head, it's vital to use naming conventionsThe most common ones are the camelCase (start with a lowercase letter and capitalize each subsequent word) or use underscores to separate words. Most importantly, the names should be descriptive and consistent throughout the project, avoiding generic names like var1 o dato2.
The correct definition of names, respect for reserved words, and the avoidance of restricted underlining patterns ensure that the code is portable and free of technical conflicts, allowing the program structure to be readable and professional.
FAQ
Can a C++ identifier start with a number?
It is not possible to start an identifier with a digit; the first character must be a letter or an underscore.
Is a variable called 'myData' the same as 'mydata'?
No, C++ is case-sensitive, so they are considered two completely different identifiers.
What happens if I use a reserved word as a variable name?
The compiler will generate a compilation error because the keywords are reserved for the exclusive use of the language.
Why is it not recommended to use a double underscore at the beginning of a name?
Identifiers that begin with two underscores are reserved for system implementation and standard libraries, and their use may cause collisions.
Can I use accents or the letter ñ in my identifiers?
The use of special characters such as accents or the letter ñ is not allowed, as only letters from the English alphabet must be used.
What is a qualified identifier in C++?
It is the one that uses the :: operator to specify the namespace or class to which the entity belongs, as is the case with std::string.
Is the dollar sign valid in all C++ compilers?
It is not a general standard, but the Microsoft compiler (MSVC) does allow the use of the $ sign as a valid character in identifiers.
Table of Contents



