Key words
Understanding Keywords in C++
Keywords are reserved words in C++ that have a predefined meaning in the language. These words cannot be used as identifiers (like variable names, function names, or class names) because they serve a special purpose within the language’s syntax. C++ is a rich, complex language, and its keywords define the core structure of the language, such as data types, control structures, and object-oriented programming concepts.
In this article, we’ll explore the most important C++ keywords, breaking them down into categories for clarity.
Complete List of C++ Keywords
The following is a complete list of reserved keywords in the C++ language. These keywords were introduced in C++ and are currently part of the language's syntax. They cannot be used as identifiers (variable names, function names, etc.).
The keywords are divided into groups based on the C++ version in which they were introduced:
- C++98 (and earlier) contains the keywords available from the beginning.
- C++11 introduces a significant number of new keywords.
- C++14 and C++17 do not introduce any new keywords.
- C++20 introduces a few new keywords for concepts and coroutines.
Keywords in C++98 (and earlier)
and, and_eq, asm, auto, bitand, bitor, break, case, catch, char, class, compl, const, const_cast, continue, decltype, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, not, not_eq, operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq
Keywords in C++11
alignas, alignof, auto, constexpr, decltype, delete, nullptr, noexcept, static_assert, thread_local
Keywords in C++14
No new keywords were introduced in C++14.
Keywords in C++17
No new keywords were introduced in C++17.
Keywords in C++20
concept, co_await, co_return, co_yield, requires
Keywords Introduced in C++23
- import: Used to include modules, facilitating better modularity and faster compilation times. :contentReference[oaicite:0]{index=0}
- module: Defines a module, a collection of code and data that can be imported by other parts of a program. :contentReference[oaicite:1]{index=1}
- pre (anticipated): Expected to be introduced in C++26, likely related to preconditions or similar concepts. :contentReference[oaicite:2]{index=2}
- post (anticipated): Also expected in C++26, likely pertaining to postconditions or related features. :contentReference[oaicite:3]{index=3}
- trivially_relocatable_if_eligible (anticipated): A proposed keyword for C++26, potentially indicating trivially relocatable types under certain conditions. :contentReference[oaicite:4]{index=4}
Keywords Introduced in C++26 (Anticipated)
- hazard_pointer (anticipated): A proposed feature for lock-free concurrent programming, allowing threads to safely access shared objects. :contentReference[oaicite:5]{index=5}
- rcu (anticipated): User-space Read-Copy-Update, a synchronization mechanism aimed at improving performance in multithreaded applications. :contentReference[oaicite:6]{index=6}
1. Control Flow Keywords
Control flow keywords help manage the execution flow of programs. These keywords determine how loops, conditionals, and function execution are handled in C++ programs.
- if: Used to make decisions based on conditions. The if statement executes a block of code only if the specified condition is true.
if (x > 0) { // do something }
- else: Follows an if statement and provides an alternative block of code to execute if the condition is false.
if (x > 0) { // do something } else { // do something else }
- switch: Used for multi-way branching, allowing you to execute different blocks of code depending on the value of a variable.
switch (x) { case 1: // do something break; case 2: // do something else break; default: // default case break; }
- case, default: Used within a switch block to define specific cases and a default case if none of the cases match.
- for, while, do: These keywords define looping constructs:
* for: Iterates over a range of values.
for (int i = 0; i < 10; ++i) { // loop body }
* while: Repeats a block of code while a condition is true.
while (x < 10) { // loop body }
* do: Similar to while, but ensures the loop body is executed at least once.
do { // loop body } while (x < 10);
- break: Exits from a loop or switch statement early.
while (true) { if (x == 5) { break; // exit the loop } }
- continue: Skips the current iteration of a loop and proceeds with the next iteration.
for (int i = 0; i < 10; ++i) { if (i == 5) { continue; // skip the rest of the loop when i is 5 } }
2. Data Type Keywords
Data type keywords define the type of data that can be stored in variables. C++ offers both built-in primitive types and user-defined types, but these keywords are used for the built-in ones.
- int: Represents an integer data type.
int x = 5;
- char: Represents a single character.
char c = 'A';
- float, double: Used for floating-point numbers (single and double precision).
float f = 3.14f; double d = 3.14159;
- bool: Represents a boolean value (true or false).
bool flag = true;
- void: Indicates a function does not return a value or specifies an empty type for pointers.
void function() { // no return value }
3. Storage Class Keywords
Storage class keywords define the scope, lifetime, and visibility of variables and functions.
- static: Used to declare variables that persist across function calls or limit the visibility of a variable to the current translation unit.
static int count = 0; // persists across function calls
- extern: Declares variables or functions that are defined in another file.
extern int count; // count is defined elsewhere
- register: Suggests that the variable be stored in a CPU register for faster access (modern compilers often ignore this).
register int count;
- mutable: Allows a member of a const object to be modified.
class MyClass { public: mutable int x; };
4. Object-Oriented Programming Keywords
C++ is an object-oriented language, and these keywords define various object-oriented constructs.
- class: Declares a class, which is a blueprint for creating objects.
class MyClass { public: int x; void display() { std::cout << x << std::endl; } };
- struct: Declares a structure, similar to a class, but with public members by default.
struct MyStruct { int x; };
- public, private, protected: Access modifiers that define the accessibility of class members.
class MyClass { private: int x; public: void setX(int val) { x = val; } };
- virtual: Used to declare a virtual function, enabling dynamic polymorphism.
virtual void display();
- override: Used in derived classes to indicate a function overrides a base class method.
void display() override;
- new, delete: Used for dynamic memory allocation and deallocation.
int* p = new int(5); // allocate memory delete p; // free memory
- this: Refers to the current instance of the class.
class MyClass { public: void print() { std::cout << this << std::endl; // address of the current object } };
- friend: Allows a non-member function or class to access the private and protected members of a class.
class MyClass { private: int x; public: friend void show(MyClass&); };
5. Other Keywords
These keywords serve various purposes throughout the language.
- return: Used to return a value from a function.
return 5;
- const: Declares constant values or variables that cannot be changed after initialization.
const int x = 10;
- sizeof: Returns the size (in bytes) of a variable or data type.
std::cout << sizeof(int); // output the size of an int
- typeid: Used for runtime type identification.
typeid(x).name();
- namespace: Defines a scope that can contain identifiers (like variables, functions, or classes) to avoid name conflicts.
namespace MyNamespace { int x = 5; }
- template: Used to define a generic class or function.
template <typename T> T add(T a, T b) { return a + b; }
Conclusion
C++ keywords form the foundation of the language’s syntax and control structures. Understanding these keywords is essential for writing correct and efficient C++ programs. From managing control flow to defining data types and implementing object-oriented principles, the variety of keywords enables a flexible and powerful programming environment. As you continue learning and practicing C++, you’ll encounter more advanced uses of these keywords, including templates, lambda functions, and type inference.