What are the Rules for Using an Underscore in a C++ Identifier?
Last Updated : 29 May, 2024
In C++, an identifier is a unique name that is used to define a variable, function, class or object, and etc in a program. It can consist of numbers, letters, and underscores and must start with an underscore or letter (can be uppercase or lowercase). No special character is allowed in identifiers. In this article, we will learn the rule about using an underscore in a C++ identifier.
Rules for Using Underscore (_) in C++ Identifier
The following are the rules that we should handle while using an underscore in a C++ identifier:
1. Leading Underscore Followed by a Lowercase Letter
In C++, an identifier can start with an underscore followed by a lowercase letter but such identifiers are reserved for implementations in the global namespace.
Example:
int _variable;
// Incorrect usage in global namespace
2. Trailing Underscore
Identifiers ending with a single underscore are not specifically reserved, but it's a common convention to use them for private member variables in classes. It’s a good practice to avoid ending identifier names with an underscore for general purposes.
Example:
int variable_;
// Valid, commonly used for private members
3. Double Underscore Anywhere
Identifiers containing adjacent double underscores are reserved for the compiler and its libraries in all scopes. Using these can interfere with compiler operations and library functionality.
Example:
int __variable;
// Invalid: Reserved for implementation
4. Leading Underscore Followed by an Uppercase Letter
Identifiers starting with an underscore followed by an uppercase letter are reserved for use in all scopes. This is done to avoid conflicts with macros and other identifiers in the standard library.
Example:
int _Variable;
// Invalid: Reserved for implementation
5. Single Underscore
Identifiers consisting solely of a single underscore are allowed, especially in local scopes, but their use is discouraged. They are typically used as placeholders or in very short code segments. However, they can be unclear in their purpose and can potentially conflict with compiler extensions.
Example:
int _;
// Discouraged: Potentially unclear and error-prone
Conclusion
All of the above rules and conventions regarding the use of the underscores in C++ identifiers are based on the guidelines established by the C++ standard to ensure that the identifiers you use are portable, understandable, and free of conflicts with compiler or system-level names. It's always best practice to follow these rules strictly, especially when writing library code or code that needs to be highly portable.
Similar Reads
What Are the Basic Rules and Idioms for Operator Overloading in C++? In C++, operator overloading is a form of compile-time polymorphism where an operator is redefined to provide a special meaning beyond its typical operation. It allows developers to use traditional operators with user-defined types (or classes). In this article, we will learn the basic rules and idi
3 min read
Why it is important to write "using namespace std" in C++ program? In this article, we will discuss the use of "using namespace std" in the C++ program.Need of Namespace in C++As the same name can't be given to multiple variables, functions, classes, etc. in the same scope. So, to overcome this situation, namespace is introduced.ExampleBelow is the C++ program illu
4 min read
Why Const is Used at the End of Function Declaration in C++? In C++, the const keyword is used to define the constant values that cannot be changed during the execution of the program. Once a value or a function is declared as constant then its value becomes fixed and cannot be changed and if we try to change it, an error is thrown. In this article, we will l
2 min read
Overloads of the Different References in C++ This article focuses on function/method overloads by references, as well as the types of arguments that can be passed. Prerequisites: l-value references.r-value references.Move semantics - std::move(). Overview:l-value refers to a memory location that identifies an object. r-value refers to the data
12 min read
Where and Why Do I Have to Put the 'template' and 'typename' Keywords? In C++, we have a template keyword to define template classes or functions and allow them to operate with generic types. On the other hand, we have typename keyword which is used to specify that the identifier that follows is a type, particularly in template code to clarify the ambiguity but confusi
3 min read