π C++ Keywords β Reserved Words Every Programmer Must Know
π§² Introduction β What Are Keywords in C++?
Keywords in C++ are reserved words that have special meaning to the compiler. These words are part of the C++ language syntax and cannot be used as names for variables, functions, classes, or identifiers. They play a fundamental role in defining the structure and behavior of C++ programs.
π― In this guide, youβll learn:
- What keywords are and why they matter
- The full list of C++ keywords (updated for C++23)
- How keywords are used in actual programs
- Common beginner mistakes to avoid
π What Is a Keyword in C++?
A keyword is a built-in command or symbol that tells the C++ compiler to perform a specific task or recognize a syntactic construct.
int main() {
return 0;
}
In the code above:
int
andreturn
are keywordsmain
is an identifier, not a keyword
π§Ύ List of C++ Keywords (Including C++11 β C++23)
πΉ Category | π Keywords |
---|---|
Data Types | int , char , float , double , bool , void , wchar_t |
Control Statements | if , else , switch , case , default , break , continue , goto , do , while , for |
Modifiers | const , static , mutable , volatile , register , extern , inline |
Classes/OOP | class , struct , union , enum , public , private , protected , virtual , friend , this , new , delete |
Functions | return , try , catch , throw , noexcept , explicit , operator , override , final |
Templates | template , typename , using , namespace |
Boolean & Null | true , false , nullptr |
Type Operators | sizeof , typeid , decltype , alignof |
Coroutines (C++20) | co_await , co_yield , co_return |
Modern C++ | constexpr , consteval , constinit , concept , requires , static_assert , import , module |
β Total: Over 95 keywords, updated with each standard release.
π§ͺ Example β Keywords in Use
#include <iostream>
int main() {
const int maxMarks = 100; // const is a keyword
int score = 85;
if (score < maxMarks) {
std::cout << "You passed!" << std::endl;
}
return 0;
}
π In this example:
int
,const
,if
, andreturn
are keywords.main
,maxMarks
, andscore
are identifiers.
β οΈ Common Mistakes with Keywords
β Mistake | β Correction |
---|---|
Using a keyword as a variable | int int = 5; β β Illegal |
Typing keyword in uppercase | Return β return (C++ is case-sensitive) |
Assuming main is a keyword | main is not a keywordβit’s a special name |
Trying to redefine keywords | Cannot define int or class as identifiers |
π Summary β Recap & Next Steps
π Key Takeaways:
- Keywords are reserved words with predefined purposes in C++
- You cannot use them as names for variables or functions
- Learn and recognize all standard keywords, especially as C++ evolves
- Understanding keyword usage is essential for valid program syntax
βοΈ Real-World Relevance:
A deep understanding of keywords helps prevent syntax errors, improves code readability, and ensures that your programs follow modern C++ standards.
β FAQs β C++ Keywords
β Are C++ keywords case-sensitive?
β
Yes. int
is valid, but Int
is treated as an identifier.
β Can I use keywords as variable names?
β No. Keywords are reserved and cannot be redefined or reused.
β Is main
a keyword?
β No. It’s a special function name, not a keyword.
β What are new keywords in C++20 and C++23?
β
Examples include concept
, requires
, consteval
, co_await
, and module
.
β How many keywords are there in modern C++?
β
Over 95 keywords, with additions in every major C++ standard.
Share Now :