Popular Google Pages:
◕ This article is regarding Rules For Variable Names in C++.
Last updated on: 24th November 2016.
◕ We should use the useful and meaningful names for variables. So that, we can understand them after a long days or other person can understand it easily. The C++ compiler doesn’t care about any variable names as long as the variable names satisfy the rules. But still we should use variable names to fit your own needs, preferences, and personal style.
For an example:
If a variable represents the number of indian states, then it is better to write it as
◕ There are few rules which we have to follow while naming the variable in C++.
These rules are:
1. Alphabetic characters, Numeric digits and the underscore (_) are allowed in the name.
2. The first character in a name cannot be a numeric digit.
3. Names are case sensitive. Uppercase characters are considered distinct from lowercase characters.
4. We can’t use any C++ keywords for a name.
5. Names beginning with two underscore characters or with an underscore character followed by an uppercase letter are reserved by the compiler and the resources it uses.
6. Names beginning with a single underscore character are reserved for use as global identifiers.
7. C++ places no limits on the length of a name. But some platforms might have their own length limits.
◕ Some Examples:
Here are some valid and invalid C++ names examples:
int india; // Valid
int India; // Valid
int INDIA; // Valid
Int india; // Invalid, because it has has to be int, not Int
int india29; // Valid int _India29; // Valid but reserved
int 29india; // Invalid, because starts with a digit
int double; // Invalid, because double is a C++ keyword
int indian_states; // Valid
int __india; // Valid but reserved,( two underscores)
int the_number_states_in_india_is_29; // Valid
int indian-states; // Invalid, because Hyphen is not allowed
int theNumberOfIndianStates; // Valid
◕ Please note:
There is also a style to describe the Names of a Variables.
Here the int prefix is used to represent the Variable Type. This is useful when we read the C++ codes after a long time or when the type of that variable is not immediately in our hand. Please also remember, this can increase your file size.
◕ Read Related Articles:
Variable in C++
Fundamental Data Types & Compound Data Types
Top of the page