Keywords in C++
Keywords in C++ are reserved words that have a predefined meaning in the language. These words are used to perform specific tasks, so they cannot be used as variable names, function names, or class names.
For example, words like int, if, while, and return are C++ keywords because the compiler already knows their purpose.
Example
int age = 20;
if (age >= 18) {
cout << "Eligible";
}
In this example:
intis a keyword used to declare an integer variable.ifis a keyword used for decision-making.
C++ Keywords List
Below are some of the most commonly used C++ keywords.
| Keyword | Purpose |
|---|---|
int | Declares an integer variable |
float | Declares a floating-point variable |
char | Declares a character variable |
bool | Declares a boolean variable |
if | Executes code based on a condition |
else | Executes when the if condition is false |
switch | Selects one block from multiple options |
case | Defines a case inside a switch statement |
for | Creates a loop |
while | Repeats code while a condition is true |
do | Executes a loop at least once |
break | Exits a loop or switch statement |
continue | Skips the current loop iteration |
return | Returns a value from a function |
void | Indicates no return value |
class | Defines a class |
public | Public access specifier |
private | Private access specifier |
protected | Protected access specifier |
const | Declares a constant value |
Note: Modern versions of C++ contain more than 90 reserved keywords. You don’t need to memorize all of them. As you learn C++, you’ll naturally become familiar with the most commonly used ones.
Can You Use Keywords as Variable Names?
No. Since keywords already have a predefined meaning, they cannot be used as identifiers.
Invalid Example
int return = 10;
The above code will generate a compilation error because return is a reserved keyword.
Valid Example
int marks = 90;
Here, marks is a valid identifier because it is not a keyword.
Key Points
- Keywords are reserved words in C++.
- Every keyword has a predefined meaning.
- Keywords cannot be used as variable or function names.
- Modern C++ provides more than 90 reserved keywords.
- You only need to remember the commonly used keywords while learning.
Frequently Asked Questions
Q1. What are keywords in C++?
Keywords in C++ are reserved words with predefined meanings. They are part of the language syntax and cannot be used as identifiers such as variable or function names.
Q2. How many keywords are there in C++?
Modern C++ includes more than 90 keywords, with the exact number depending on the C++ standard version. Common examples include int, if, while, return, and class.
Q3. Can I use C++ keywords as variable names?
No. Since keywords are reserved by the compiler, they cannot be used as variable names, function names, or class names in a C++ program.
Q4. What is the difference between keywords and identifiers in C++?
Keywords have predefined meanings in the C++ language, whereas identifiers are user-defined names given to variables, functions, classes, and other program elements.
Q5. Why are C++ keywords important?
C++ keywords define the structure and behavior of a program. Understanding them helps you write correct syntax, avoid compilation errors, and build a strong foundation in C++ programming.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
