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.
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:
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
alignas, alignof, auto, constexpr, decltype, delete, nullptr, noexcept, static_assert, thread_local
No new keywords were introduced in C++14.
No new keywords were introduced in C++17.
concept, co_await, co_return, co_yield, requires
Control flow keywords help manage the execution flow of programs. These keywords determine how loops, conditionals, and function execution are handled in C++ programs.
Used to make decisions based on conditions.
if (x > 0) {
// do something
}
if (x > 0) {
// do something
} else {
// do something else
}
switch (x) {
case 1:
// do something
break;
case 2:
// do something else
break;
default:
// default case
break;
}
for (int i = 0; i < 10; ++i) {
// loop body
}
while (x < 10) {
// loop body
}
do {
// loop body
} while (x < 10);
while (true) {
if (x == 5) {
break; // exit the loop
}
}
for (int i = 0; i < 10; ++i) {
if (i == 5) {
continue; // skip the rest of the loop when i is 5
}
}
int x = 5;
char c = 'A';
float f = 3.14f;
double d = 3.14159;
bool flag = true;
void function() {
// no return value
}
static int count = 0;
extern int count;
register int count;
class MyClass {
public:
mutable int x;
};
class MyClass {
public:
int x;
void display() {
std::cout << x << std::endl;
}
};
struct MyStruct {
int x;
};
class MyClass {
private:
int x;
public:
void setX(int val) {
x = val;
}
};
virtual void display();
void display() override;
int* p = new int(5);
delete p;
class MyClass {
public:
void print() {
std::cout << this << std::endl;
}
};
class MyClass {
private:
int x;
public:
friend void show(MyClass&);
};
return 5;
const int x = 10;
std::cout << sizeof(int);
typeid(x).name();
namespace MyNamespace {
int x = 5;
}
template <typename T>
T add(T a, T b) {
return a + b;
}
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.