The rules every C++ variable name must follow
A variable name, which C++ calls an identifier, has to obey a few hard rules. If you break one, the program does not compile.
- Allowed characters. Letters, digits and the underscore
_. Nothing else: no spaces, hyphens, dots or symbols such as#. - The first character cannot be a digit. It must be a letter or an underscore.
- Keywords are off limits. Words that have a meaning in the language, such as
int,class,return,new,ifandwhile, cannot be used as names. - Names are case sensitive.
name,NameandNAMEare three different variables.
There is no practical limit on the length of a name. Many compilers also accept letters from other alphabets, but ASCII letters are the safe choice.
Names that do not compile
int 18spam = 1; // starts with a digit
string first name = "Kate"; // contains a space
bool #name = true; // # is not allowed
int my-var = 1; // the hyphen is read as a minus sign
int class = 1; // class is a keyword
The hyphen case is worth understanding. The compiler does not see a bad name there. It sees my followed by a minus and var, which does not make sense in a declaration.
Names that work
int spam18 = 1;
string first_name = "Kate";
bool isReady = true;
int count2 = 0;
Names to avoid even though they compile
Some names are accepted by the compiler but are reserved for the C++ implementation, which means they may clash with names that the compiler or the standard library define. Avoid these:
- names that contain two underscores in a row, such as
__x; - names that start with an underscore followed by an uppercase letter, such as
_Count; - names that start with an underscore at global scope, outside any function.
The simplest habit is to never start a name with an underscore.
A second trap comes from using namespace std;, which makes the standard library's names visible everywhere. If you declare a global variable called count and your file includes <algorithm>, which declares std::count, then using count gives an error saying that the reference is ambiguous. The same can happen with other common names, such as distance, max and list. A local variable named count inside a function is fine, because it hides the standard one. Descriptive names such as wordCount avoid the issue altogether.
Naming conventions: camelCase and snake_case
A name often needs several words, such as the first name of a person. Spaces are not allowed, so the words have to be joined in some way. Two conventions are common:
- camelCase starts with a lowercase letter, and every later word starts with a capital:
firstName,yearsOfExperience. - snake_case uses lowercase letters and joins the words with underscores:
first_name,years_of_experience.
The compiler accepts both. C++ has no single official style: the standard library itself uses snake_case (push_back, unordered_map), while many codebases use camelCase for variables. This lesson uses camelCase. Whichever you pick, use it everywhere in the same program. A file with first_name next to lastName still runs, but it is harder to read.
Other habits that are common in C++ code:
- Types and classes start with a capital letter:
Solution,ListNode. - Boolean variables read like a yes or no question and often start with
isorhas:isEmployed,hasCycle. - Private data members of a class are often marked with a trailing underscore (
count_) or anm_prefix, so they are not mixed up with parameters. This is a team convention, not a language rule.
Case sensitivity
string name = "Kate";
string Name = "Andy";
string NAME = "George";
cout << name << " " << Name << " " << NAME << endl;
Kate Andy George
All three variables are valid and independent. Telling variables apart by case alone is legal, but it causes bugs, because one mistyped capital letter silently refers to a different variable. A mistyped name that matches nothing at least fails to compile.
Choose names that explain themselves
The compiler does not care what you call a variable, but the next person to read the code does. Compare these two versions:
int a = 25;
int b = 13;
int c = a * b;
cout << c;
int hours = 25;
int rate = 13;
int pay = hours * rate;
cout << pay;
Both print 325. Only the second says what the number means. Short names such as i or x are fine for a loop counter or a small calculation, but anything that lives for more than a few lines should describe its content.
The exercise, step by step
The lesson asks for three declarations, all in camelCase:
firstName, astringset to"John". The two words are joined, and the second one starts with a capital.isEmployed, aboolset totrue. Theisprefix makes the name read like a question the value answers.yearsOfExperience, anintset to5. Three words, with the first one lowercase.
string firstName = "John";
bool isEmployed = true;
int yearsOfExperience = 5;
Printing the three variables with cout shows John 1 5. A bool prints as 1 for true and 0 for false, unless you write cout << boolalpha first, which prints the words true and false.
Key takeaways
- A name may contain letters, digits and underscores, and it must not start with a digit.
- Keywords cannot be names, and names are case sensitive.
- Avoid names with a leading underscore or a double underscore, because they are reserved.
- Pick camelCase or snake_case and stay consistent.
- Prefer descriptive names over single letters, except for short-lived loop variables.
