Start learning free

LESSON 11 OF 409 · VARIABLES

Variable Naming Rules in C++: Valid Names and Conventions

Lesson Read the full explanation

TL ; DR:

  • In C++, we use the camelCase convention to name our variables. It implies separating different words with capital letters, like this:

    string companyName = "Apple";
    
    int foundingYear = 1976;
    
    bool isUnicorn = true;
    
    int numberOfEmployees = 137000;
    



Full lesson:

Variable names must consist ONLY of letters, numbers and underscores and they must start with a letter or underscore.

Bad variable names:

int 18spam;
string x.15;
bool #name;

If you try to create a variable with any of these names, C++ will throw an error.


Naming conventions: camelCase and snake_case

Sometimes a variable name might consist of multiple words, for example if you want to store the first name of a person. You might be tempted to separate those names by whitespaces, like this:

string first name = "Kate";

This code will also throw an error.

When you name your variables, you want to use one of these two conventions:

snake_case = separate different words with an underscore (_), like this:

string first_name = "Jane";
string last_name = "Doe";

camelCase = separate different words with capital letters, like this:

string firstName = "Jane";
string lastName = "Doe";

It's important to be consistent with the casing technique you use. Do not combine both of them in the same code, like this:

string first_name = "Jane";
string lastName = "Doe";

Case sensitive:

Variable names are case sensitive:

string name = "Kate";
string Name = "Andy";
string NAME = "George";

cout << name << " " << Name << " " << NAME; // Output: Kate Andy George

Although C++ allows us to do this, it's always confusing and a bad coding practice to differentiate variable names just by case!


Mnemonic Variable Names:

Since we programmers are given a choice in how we choose our variable names, there is a bit of "best practice".

Sometimes we want them short like x and y, but sometimes we want them descriptive, especially if they are important.

Here is a great example:

int a = 25;
int b = 13;
int c = a * b;
cout << c;

compared to:

int hours = 25;
int rate = 13;
int pay = hours * rate;
cout << pay;

C++ understands and is happy with both of these pieces of code. But it's clearly which version makes your reader happier.

Assignment
Follow the Coding Tutorial and let's name some variables well!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    C++ · VARIABLES

    Variable Naming Rules in C++, explained

    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, if and while, cannot be used as names.
    • Names are case sensitive. name, Name and NAME are 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 is or has: isEmployed, hasCycle.
    • Private data members of a class are often marked with a trailing underscore (count_) or an m_ 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:

    1. firstName, a string set to "John". The two words are joined, and the second one starts with a capital.
    2. isEmployed, a bool set to true. The is prefix makes the name read like a question the value answers.
    3. yearsOfExperience, an int set to 5. 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.
    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free