What are the rules for using an Underscore in a C++ dentifier?
An identifier in C++ is a special name that is used within a program to specify variables, functions, classes, objects, and other things. It must begin with an underscore or letter (uppercase or lowercase), and it can contain numbers, letters, and underscores. Special characters are not permitted in identifiers. The guideline for using an underscore in a C++ identifier will be covered in this article. It can be used in many different fields, including software development, game development, embedded programming, and system programming. It enables programmers to develop fast, low-level memory-accessible programs.
Identifiers are user-defined names in C++ for functions, variables, classes, objects, etc. An identifier must begin with a letter, either uppercase, lowercase, or an underscore. It can also contain numerals and other characters.
Although there are no restrictions on how we can use underscores in our identifiers, we must adhere to the following common naming standards in C++ when doing so:
Since underscores are typically used to denote system-level names and operating system characteristics, we should refrain from using them at the beginning of identifier names. However, we may encounter a disagreement because different C++ compilers and libraries utilize identifiers that start with an underscore. An underscore should not be used to conclude identifier names. In order to maintain consistency in our code's visual design and to make it easier to read, underscores at the end of an identifier are not advised, even when libraries and compilers do not reserve this standard. Two consecutive underscores at the beginning of identifier names should be avoided. For instance, foo is likewise set aside. With a few exceptions listed above, using underscores in identifiers is hassle-free. Underscores are generally recommended to be used to distinguish between words in an identifier. For instance, the "Snake case" involves my_identifier.
Rules for Using Underscore (_) in C++ Identifier:
When utilizing an underscore in a C++ identifier, we must adhere to the following rules:
1. The leading underscore follows a lowercase letter:
An identifier in C++ may begin with an underscore and then a lowercase letter, although these identifiers are only allowed for use in global namespace implementations.
Example:
incorrect use of int _variable in the global namespace
2. The Trailing Underscore:
Although single underscore identifiers are not reserved, they are customary for private member variables in classes. For general purposes, it is best practice to refrain from using an underscore at the end of identifier names.
Example:
int variable_; / True, frequently utilized for individual members
3. Double Underscore Anywhere:
In all scopes, identifiers with consecutive double underscores are reserved for the compiler and its libraries. Utilizing these may cause issues with library functioning and compiler processes.
Example:
Invalid: Reserved for implementation, int __variable;
4. The leading underscore follows an uppercase letter:
Identifiers that begin with a lowercase letter and an underscore are reserved for usage across all scopes. By doing this, conflicts with other identifiers in the standard library and macros are avoided.
Example:
int _Variable; // Invalid: Set aside for use during execution
5. Just One Underscore:
Although identifiers are permitted, especially in local scopes, identifiers with just one underscore are not advised. They are usually utilized in extremely brief code parts or as placeholders. On the other hand, their purpose may only sometimes be evident, and they might clash with compiler extensions.
Example:
// Discouraged: May be imprecise and prone to errors
Code Implementation:
Let us take an example to illustrate the different rules for using an underscore in a C++ identifier.
#include <iostream> using namespace std; int main() { int _var1 = 5; int var2_ = 6; int var_3 = 7; cout<<"starting underscore = " << _var1 << endl; cout<<"end underscore = " << var2_ << endl; cout<<"underscore middle = " << var_3 << endl; return 0; }
Output:
starting underscore = 5 end underscore = 6 underscore middle = 7
Conclusion:
In conclusion, all of the conventions mentioned above and restrictions regulating the usage of underscores in C++ identifiers are based on the principles provided by the C++ standard to ensure that the identifiers we use are portable, understandable, and free of conflicts with compiler or system-level names. Strict adherence to these guidelines is often advised, particularly when developing library code or other programs that must be extremely portable.