C++ Basics: Keywords and Identifiers

Learn what reserved keywords are, how to name your own identifiers, and the rules and conventions to follow.

C++ Keywords

Keywords are reserved words that have a fixed meaning in the C++ language. You cannot use them as variable or function names. The C++17 standard defines 84 keywords.

Keywords

Every keyword has a specific syntactic role; the compiler treats them differently from ordinary identifiers.

  • Keywords are always lowercase: int, return, class, if
  • You cannot redefine or shadow a keyword
  • Common keywords: int, float, double, char, bool, void, if, else, for, while, return, class, struct, new, delete, true, false, nullptr
  • C++11 added: auto, nullptr, constexpr, decltype, override, final, noexcept
CategoryKeywords
Data typesint, float, double, char, bool, void, auto
Control flowif, else, switch, case, break, continue, return
Loopsfor, while, do
OOPclass, struct, public, private, protected, virtual, this
Memorynew, delete, nullptr
Modern C++constexpr, decltype, override, final, noexcept

Naming Identifiers

An identifier is any name you give to a variable, function, class, or other program entity. Good identifiers are descriptive and follow consistent conventions, making code much easier to read.

Identifier Rules

An identifier must start with a letter or underscore, contain only letters, digits, or underscores, and must not be a keyword.

  • Valid starts: a letter (a-z, A-Z) or underscore (_)
  • Valid continuation: letters, digits (0-9), or underscores
  • Invalid: spaces, hyphens, dollar signs, or starting with a digit
  • Identifiers starting with double underscore (__) are reserved for the compiler

Valid and Invalid Identifiers

C++

The commented lines show what would cause compile errors.

Identifier Best Practices

C++ communities follow consistent naming conventions to make code readable across teams and projects.

Naming Conventions

Consistent naming is a team contract, it lets anyone read the code and instantly know what kind of thing an identifier refers to.

  • Variables and functions: camelCase (playerScore, getHealth)
  • Classes and structs: PascalCase (PlayerData, GameEngine)
  • Constants and macros: ALL_CAPS (MAX_SIZE, PI)
  • Private members: trailing underscore or m_ prefix (name_ or m_name)
  • Avoid single-letter names except loop counters (i, j, k)

Naming Conventions in Action

C++

Constants, classes, and variables each follow a distinct convention.

Knowledge Check

1. Which of the following is a valid C++ identifier?

2. Can you use a C++ keyword as a variable name?

3. Which character can an identifier start with?

4. How many keywords does the C++17 standard define (approximately)?

5. Which naming convention is commonly used for C++ class names?