Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 171 Β· Getting Started

Keywords and Identifiers

See every C++ keyword grouped by purpose, learn the rules for valid identifiers, compare naming styles and avoid names reserved for the compiler.

Every program is built from two kinds of words. Keywords are words the language itself owns, like int, if and return; each has a fixed meaning, and you can't use them for anything else. Identifiers are the names you invent: names for variables, functions, types and namespaces, such as total_price or Customer.

Choosing good names is one of the most important skills in programming, because code is read far more often than it's written. In this lesson you'll see the full list of C++ keywords organized by what they do, learn the exact rules an identifier must follow, compare the naming styles used in real C++ projects, and learn which names are secretly off-limits because the compiler and standard library reserve them.

What is a keyword?

A keyword is a reserved word with a meaning defined by the C++ standard. The compiler recognizes it everywhere, so it can't be the name of a variable or function. Run this example; it fails on purpose:

C++ A keyword can't be a name (on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

GCC reports error: expected unqualified-id before 'new'. That's compiler language for "I expected a name here, but new is a keyword". new creates objects in dynamic memory, so it can never be a variable name. Rename it to new_count or fresh and the program compiles.

Keywords are always lowercase. Because C++ is case-sensitive, New and NEW are not keywords and are technically allowed as names, but using them is confusing, so don't.

C++ keywords by purpose

C++23 has 81 keywords. You don't need to memorize them: you'll learn each one in the lesson where it's used. Grouping them by purpose shows the shape of the whole language:

Purpose Keywords
Fundamental types bool, char, char8_t, char16_t, char32_t, wchar_t, short, int, long, signed, unsigned, float, double, void
Type deduction auto, decltype
Values true, false, nullptr, this
Control flow if, else, switch, case, default, for, while, do, break, continue, goto, return
Constants and compile time const, constexpr, consteval, constinit, static_assert
Storage and linkage static, extern, thread_local, inline, mutable, volatile, register
Names and aliases namespace, using, typedef
User-defined types class, struct, union, enum
Classes and access public, private, protected, virtual, friend, explicit, operator
Templates and concepts template, typename, concept, requires
Memory and objects new, delete, sizeof, alignof, alignas
Casts and type info static_cast, dynamic_cast, const_cast, reinterpret_cast, typeid
Exceptions try, catch, throw, noexcept
Coroutines co_await, co_yield, co_return
Modules export
Low level asm

A few notes on this table:

  • Some keywords do more than one job. default labels the default case in a switch and also requests compiler-generated functions (= default); delete frees memory and also removes functions (= delete); using creates aliases and also brings names into scope; static has several meanings depending on where it appears.
  • register no longer does anything since C++17, and asm (inline assembly) is rarely used. They remain reserved so they can't be used as names.
  • C++26 adds contract_assert for contracts.

Identifiers with special meaning

A handful of words act like keywords only in specific places and are ordinary identifiers everywhere else. They're called contextual keywords (officially, identifiers with special meaning):

Word Special meaning
override Marks a function that overrides a virtual function.
final Prevents further overriding or inheritance.
import, module Used in C++20 module declarations.

The language made them contextual so that existing code using them as names wouldn't break. Legal or not, avoid them as names; readers will do a double take.

Alternative tokens

C++ also reserves eleven words as alternative spellings of operators, originally for keyboards that lacked symbols like & and |:

Word Means Word Means
and && bitand &
or || bitor |
not ! xor ^
not_eq != compl ~
and_eq &= or_eq |=
xor_eq ^=

They're reserved too, so you can't name a variable and or not. Some programmers like and, or and not for readability; most codebases use the symbols. Here's both, plus contextual keywords used as ordinary names:

C++ Contextual keywords and alternative tokens

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

It prints both messages, then final = 100, module = 3. Just because you can name a variable final doesn't mean you should.

Rules for identifiers

An identifier is a name you create. The rules are short:

  1. It may contain letters, digits and the underscore _.
  2. It must not start with a digit. player2 is fine; 2nd_place is not.
  3. It must not be a keyword or alternative token.
  4. It's case-sensitive: score, Score and SCORE are three different names.
  5. There's no practical length limit. Compilers handle names hundreds of characters long.
  6. Spaces and punctuation like -, . or # aren't allowed. total-price would be read as total minus price.

Since C++23, "letters" formally includes Unicode letters, so grâße or café are valid names in GCC, Clang and MSVC. Stick to plain ASCII English names unless your team agrees otherwise; they're easier to type, search and share internationally. GCC also accepts $ in names as an extension, but it isn't standard C++, so avoid it.

C++ Valid and invalid names (on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The first error, on line 7, says expected unqualified-id before numeric constant: when the compiler sees 2, it thinks a number has started, not a name. The second error, on line 9, is stranger: unable to find numeric literal operator 'operator""nd_place'. The compiler read 2nd_place as the number 2 followed by a suffix nd_place, the way 10u is 10 with the suffix u. Rename it to second_place in both places and the program prints 10 120 140 2.

Naming conventions

The compiler only cares that a name is legal. Humans care that it's readable and consistent. Unlike Java or C#, C++ has no single official style, so you'll meet several:

Style Example Where you'll see it
snake_case max_speed, read_file The C++ standard library (std::vector, push_back, string_view), Boost, many open-source projects
camelCase maxSpeed, readFile Qt, many company codebases, code by Java/JavaScript developers
PascalCase MaxSpeed, ReadFile Type names in most styles (Customer, HttpClient); functions in Google and Unreal Engine style
SCREAMING_SNAKE_CASE MAX_BUFFER_SIZE Macros (#define) almost everywhere
k prefix kMaxSpeed Constants in the Google C++ style

This course follows a common modern mix that matches the standard library:

  • Variables and functions: snake_case (total_price, print_report).
  • Types (classes, structs, enums, concepts): PascalCase (BankAccount, Shape).
  • Constants: snake_case like other variables, made const or constexpr.
  • Macros: SCREAMING_SNAKE_CASE, and as few of them as possible.

Whichever style you use, consistency within a project matters more than the style itself. When you join an existing codebase, follow its rules.

Choosing good names

Good names make comments unnecessary. A few guidelines:

  • Say what the value means, not its type: seconds_elapsed instead of int1 or number.
  • Include units when they matter: timeout_ms, width_px, distance_km.
  • Booleans read as yes/no questions: is_valid, has_children, can_retry.
  • Functions are verbs: calculate_total(), load_settings(). Types are nouns: Invoice, Parser.
  • Short names are fine for short scopes. i for a loop index or x, y for coordinates are clearer than long names. A variable used across a whole file deserves a descriptive name.
  • Avoid lookalikes and vague words: l vs 1, O vs 0, data2, temp, stuff.
C++ Names that explain themselves

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Even without comments, you can tell what every line does. It prints Fuel needed: 27.3 L and Needs refuel on the way: true.

Reserved names: watch the underscores

Some identifiers are legal to write but reserved for the compiler and the standard library. Using them in your own code is undefined behavior: it might work today and clash with a library name tomorrow, with a baffling error or silent misbehavior. The rules:

  • Any name containing a double underscore __ anywhere, like my__value or __count, is reserved.
  • A name starting with an underscore followed by an uppercase letter, like _Value or _Max, is reserved everywhere.
  • A name starting with an underscore at global scope (outside any function, class or namespace), like _count, is reserved.

That's why standard library headers are full of names like __first and _Tp (you saw some in compiler errors in C++ Syntax): those names are guaranteed never to clash with yours, as long as you follow the rules. The compiler usually won't warn you if you break them.

The simple, safe rule: never start a name with an underscore, and never use two underscores in a row. If you want to mark member variables, a common convention is a trailing underscore (size_, name_) or an m_ prefix (m_size), both of which are safe.

Warning: Also avoid naming your own things after common standard library names (count, max, string, vector, size) at global scope, especially if anyone might write using namespace std;. As you saw in C++ Syntax, that combination produces "ambiguous" errors.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. Which of these is a valid C++ identifier?

    point_3d uses letters, a digit and an underscore and doesn't start with a digit. 3d_point starts with a digit, total-cost contains a minus sign, and return is a keyword.
  2. 2. What happens when you compile int while = 1;?

    Keywords can't be used as identifiers. The compiler rejects the declaration.
  3. 3. A program declares int total = 5; and int Total = 7;, then prints total + Total. What is printed?

    C++ is case-sensitive, so total and Total are two different variables, and 5 + 7 is 12.
  4. 4. Which name is reserved and should not be used in your own code?

    Names starting with an underscore followed by an uppercase letter are reserved for the implementation everywhere. A trailing underscore or m_ prefix is safe.
  5. 5. Which statement about override and final is true?

    override and final are identifiers with special meaning only after a member function declaration or class name. Elsewhere they're ordinary (if confusing) names.

Summary

  • Keywords like int, if, class and return are reserved words with fixed meanings; C++23 has 81 of them, plus 11 alternative tokens such as and, or and not.
  • override, final, import and module are special only in certain places; avoid them as names anyway.
  • Identifiers use letters, digits and underscores, can't start with a digit, can't be keywords, and are case-sensitive.
  • C++ has several naming styles; this course uses snake_case for variables and functions and PascalCase for types, like the standard library's own style. Be consistent within a project.
  • Good names describe meaning and units, read naturally, and make comments unnecessary.
  • Never start a name with an underscore or use __: those names are reserved for the compiler and standard library.

Next: Variables, where you'll declare, initialize and change variables, and learn why uninitialized variables are dangerous.