If you’re just starting out with C++ programming, you’ve probably come across terms like int, if, while, and return. These aren’t just random words — they’re C++ keywords. C++ Keywords are like the building blocks of the language. They tell the compiler exactly what to do. Think of them as reserved commands that give structure and meaning to your code.
So, why are they important?
Without keywords, your C++ code wouldn’t make sense to the compiler. They help define data types, control flow, memory handling, and more.
🔒 Quick note: Keywords are reserved words, meaning you cannot use them as names for your variables, functions, or identifiers. For example, trying to name a variable
intorreturnwill throw an error.
What Are C++ Keywords ?
In simple terms, keywords in C++ are predefined, reserved words that have special meaning in the language. They form the core syntax and are used to perform specific tasks in a program.
Each keyword serves a unique purpose — from declaring variables (int, float) to controlling flow (if, while) or managing memory (new, delete).
Keywords vs Identifiers
| Aspect | Keywords | Identifiers |
|---|---|---|
| Definition | Reserved words with predefined meanings in C++ | Names created by programmers for variables, functions, arrays, etc. |
| Example | if, class, return | age, getData(), totalCount |
| Modifiable | ❌ Cannot be changed or redefined | ✅ User-defined and modifiable |
| Usage | Control structure or syntax | Represent data or functionality |
In short: Keywords are the language’s own vocabulary. Identifiers are the names you give to things in your code.
Complete C++ Keywords List (2025 Updated)
| Keyword | Meaning | Example |
|---|---|---|
auto | Type is automatically deduced by compiler | auto x = 5; |
bool | Boolean data type | bool isReady = true; |
break | Breaks out of a loop or switch | break; |
case | Used in switch statements | case 1: cout << "One"; |
catch | Catches exceptions thrown by try | catch (int e) {} |
char | Character data type | char ch = 'A'; |
class | Declares a class | class Student { }; |
const | Makes variable value constant | const int x = 10; |
continue | Skips to the next loop iteration | continue; |
default | Default case in switch | default: cout << "Other"; |
delete | Frees memory allocated by new | delete ptr; |
do | Used in do-while loop | do { } while(x); |
double | Double-precision float type | double pi = 3.14; |
else | Alternate branch of if statement | else { cout << "No"; } |
enum | Declares enumeration | enum Color { RED, GREEN }; |
explicit | Prevents implicit conversions | explicit MyClass(int); |
extern | Declares a global variable | extern int x; |
false | Boolean false value | bool isOn = false; |
float | Floating-point data type | float rate = 5.5f; |
for | Looping construct | for (int i=0; i<5; i++) |
friend | Grants access to private members | friend class Helper; |
goto | Transfers control to a label | goto label; |
if | Conditional execution | if (x > 5) { } |
inline | Suggests inlining function | inline void show() {} |
int | Integer data type | int age = 30; |
long | Long integer type | long total = 999999; |
mutable | Allows member to be modified in const object | mutable int x; |
namespace | Declares a namespace | namespace mySpace {} |
new | Allocates memory dynamically | int* ptr = new int; |
nullptr | Null pointer constant | int* p = nullptr; |
operator | Overloads an operator | operator+ |
private | Private class access specifier | private: |
protected | Protected access in class | protected: |
public | Public access in class | public: |
register | Suggests storing variable in CPU register | register int x; |
reinterpret_cast | Reinterprets bits of a value | reinterpret_cast<char*>(ptr) |
return | Returns value from function | return x; |
short | Short integer type | short val = 100; |
signed | Signed modifier for int types | signed int a; |
sizeof | Gets size of variable/type | sizeof(int) |
static | Static storage duration | static int count = 0; |
struct | Defines a structure | struct Point { }; |
switch | Multi-branch selection | switch (option) { } |
template | Template for generic programming | template<typename T> |
this | Pointer to current object | this->x = x; |
throw | Throws an exception | throw 1; |
true | Boolean true value | bool isDone = true; |
try | Starts a block for exception handling | try { } |
typedef | Alias for data types | typedef int myInt; |
typeid | Gets type information | typeid(var).name(); |
typename | Indicates a type in template | typename T::value_type |
union | Declares a union | union Data { int x; float y; }; |
unsigned | Unsigned integer type | unsigned int u = 10; |
using | Allows namespace usage or type alias | using namespace std; |
virtual | Enables polymorphism | virtual void display(); |
void | No return type | void sayHello(); |
volatile | Prevents compiler optimization | volatile int x; |
wchar_t | Wide character type | wchar_t w = L'Ω'; |
while | Repeats loop while condition is true | while (x > 0) { } |
static_cast | Converts between types safely | static_cast<float>(x) |
const_cast | Removes constness from variable | const_cast<int&>(y) |
dynamic_cast | Casts between polymorphic types | dynamic_cast<Child*>(base) |
export | Used with templates (rarely used) | export template<typename T> |
Classification of C++ Keywords
C++ keywords can be grouped into logical categories to better understand their purpose. Here’s how they’re typically classified:
1. Data Types
Used to define the type of data a variable holds.
Keywords: int, char, float, double, bool, void, wchar_t, short, long, signed, unsigned
2. Control Structures
Used for decision-making and loop control.
Keywords: if, else, switch, case, default, for, while, do, break, continue, goto, return
3. Modifiers
Alter properties of data types or variables.
Keywords: const, static, volatile, mutable, register, extern, inline
4. Access Specifiers
Define access levels for class members.
Keywords: public, private, protected, friend
5. Others
These serve various roles — exception handling, memory management, object-oriented programming, and templates.
Keywords:
- OOP:
class,this,virtual,new,delete,operator,explicit,typename,template - Exception Handling:
try,catch,throw - Type Conversions:
static_cast,dynamic_cast,const_cast,reinterpret_cast - Miscellaneous:
namespace,using,typeid,sizeof,union,struct,enum,typedef,nullptr,true,false
Examples of Commonly Used Keywords
Let’s walk through real C++ code snippets that demonstrate how these keywords are used effectively in everyday programming:
🔹 Example 1: Using Data Types and Control Structures
#include <iostream>
using namespace std;
int main() {
int age = 18;
if (age >= 18) {
cout << "Eligible to vote.";
} else {
cout << "Not eligible.";
}
return 0;
}
Best Practice: Always initialize variables and use clear conditions in if statements.
🔹 Example 2: Classes and Access Specifiers
class Person {
private:
string name;
public:
void setName(string n) {
name = n;
}
void greet() {
cout << "Hello, " << name << "!";
}
};
int main() {
Person p;
p.setName("Alex");
p.greet();
return 0;
}
Best Practice: Keep data members private and use public methods for access — this ensures encapsulation.
🔹 Example 3: Loop with for and break
for (int i = 0; i < 5; i++) {
if (i == 3)
break;
cout << i << " ";
}
Best Practice: Use break sparingly and only when it improves readability or control logic.
🔹 Example 4: Exception Handling
try {
throw 100;
} catch (int e) {
cout << "Exception caught: " << e;
}
Best Practice: Always use exception handling to manage unexpected errors without crashing the program.
Keywords Introduced in Modern C++ (C++11 and Beyond)
Modern C++ versions like C++11, C++14, C++17, and C++20 introduced several new keywords to make coding safer, clearer, and more efficient. Here are some of the most useful ones:
| Keyword | Introduced In | Purpose | Example |
|---|---|---|---|
nullptr | C++11 | Replaces NULL with a type-safe null pointer | int* p = nullptr; |
constexpr | C++11 | Declares compile-time constants | constexpr int size = 10; |
override | C++11 | Indicates a virtual function is overriding a base class method | void show() override; |
final | C++11 | Prevents further overriding of virtual functions | class A final {}; |
static_assert | C++11 | Compile-time assertion check | static_assert(sizeof(int) == 4, "Int must be 4 bytes"); |
decltype | C++11 | Inspects type of an expression | decltype(x + y) z = x + y; |
thread_local | C++11 | Creates variables local to a thread | thread_local int counter; |
noexcept | C++11 | Indicates a function doesn’t throw exceptions | void foo() noexcept; |
co_await, co_yield, co_return | C++20 | Used in coroutines for asynchronous programming | co_return 42; |
Note: These keywords enhance type safety, enable metaprogramming, and support multithreading and coroutines.
Common Mistakes Beginners Make with Keywords
Avoid these frequent beginner errors to write clean and error-free C++ code:
1. Using Keywords as Variable Names
int class = 5; // ❌ Error: 'class' is a reserved keyword
✅ Fix:
int classNumber = 5;
2. Misunderstanding const and constexpr
Many confuse these two:
constcan be evaluated at runtime.constexprmust be known at compile-time.
3. Incorrect Use of Scope
void func() {
if (true)
int x = 10; // ❌ Error in some compilers: declaration after control
}
✅ Always use {} with control structures:
if (true) {
int x = 10;
}
4. Forgetting to Use public in Classes
class Student {
void setData(); // ❌ private by default
};
✅ Fix:
class Student {
public:
void setData();
};
Our AI-powered online compiler makes coding a breeze. Instantly write, run, and test code with AI-enhanced features that simplify the entire process. Forget the hassle of traditional setups—our compiler offers seamless integration and lightning-fast execution, letting you focus on what you do best: creating amazing code.
Interview Questions on C++ Keywords
Here are beginner-friendly questions with short answers to help in interviews:
| Question | Short Answer |
|---|---|
Q1. What is the difference between const and constexpr? | const is runtime constant; constexpr is compile-time constant. |
| Q2. Can we use C++ keywords as variable names? | No, keywords are reserved and will cause compilation errors. |
Q3. What does virtual keyword do? | Enables method overriding in derived classes for polymorphism. |
Q4. What is nullptr? | A type-safe replacement for NULL introduced in C++11. |
| Q5. What are access specifiers? | Keywords that control access to class members: private, public, protected. |
Q6. Difference between struct and class? | Default access in struct is public; in class it is private. |
Q7. What is the purpose of static keyword? | Retains variable value between function calls or sets scope to class. |
Q8. Explain the inline keyword. | Suggests the compiler to insert function code at call site to optimize speed. |
Q9. What does typedef do? | Creates a new name (alias) for an existing data type. |
Q10. How is new different from malloc() in C++? | new calls constructor and is type-safe; malloc() does not. |
Conclusion
Completing ‘C++ Keywords Explained’ boosts your coding confidence, expands your tech skills, and sharpens your problem-solving abilities. Tackle real-world projects with ease. Ready to explore further? Dive into more programming languages like Java, Python, and C on Newtum and continue your coding journey.
Edited and Compiled by
This article was compiled and edited by @rasikadeshpande, who has over 4 years of experience in writing. She’s passionate about helping beginners understand technical topics in a more interactive way.