Reserved Keywords in C++
What are reserved keywords in C++?
There are a few keywords that cannot be used as identifiers as those words are reserved for some other purposes, such keywords are called reserved keywords.
Here is the list of reserved keywords in c++:
NOTE: All the keywords in c++ are written in lowercase letters.
auto | bool | break | Case | catch | char | class |
const | continue | double | Default | delete | else | enum |
explicit | friend | float | For | int | long | mutable |
new | operator | private | Protected | public | register | return |
struct | switch | short | Sizeof | static | this | typedef |
throw | true | try | Union | virtual | void | while |
Now let's discuss these keywords in detail:
- Auto: when we use the auto keyword before a variable, we don't need to mention any data type for the variable. This auto variable will set the data type according to the entered value.
Example:
// C++ code to demonstrate the use of auto keywords.
#include <bits/stdc++.h>
using namespace std;
int main(){
int num = 10;
string firm = "example";
// Auto keyword is used to set the variable data type automatically.
auto a = 10;
auto e = "Firm";
}
Explanation:
This example demonstrates the use of auto keywords in c++ reserved keywords. In this example, we have declared two variables with interger and string data type. Then we declared other two data types with the auto keyword. In case 2, the auto keyword will automatically assign the variable's data type according to the value. In both cases, we will not get any errors.
- Bool: The bool keyword is used to define the variable of Boolean data type, meaning it will only take true or false.
Example:
// C++ code to demonstrate the use of the bool keyword.
#include <bits/stdc++.h>
using namespace std;
int main(){
bool a=true;
cout<<a;
}
Output:

Explanation:
This example demonstrates the use of the bool keyword in c++. In the code, we have declared a bool variable a and assigned true to it. As it is a Boolean variable, it will only take true or false. Then, when we have printed the variable a, we will get the output as 1 as the bool expression is true. If the Boolean expression is false, then the output will be 0.
- Break: Break is a keyword used in the loop. When the break statement is executed in the loop, the whole loop gets terminated.
Example:
// C++ code to demonstrate the use of break keywords.
#include <bits/stdc++.h>
using namespace std;
int main(){
//for loop is used to show how the loop acts when the break statement is executed
for(int i=0;i<5;i++){
if(i==3){
//if the i value is 3, then the break statement will be executed and will break the loop
break;
}
else{
cout<<i<<endl;
}
}
}
Output:

Explanation:
The above code is used to demonstrate how the break keyword will work. In the code, we have used for loop to show how the loop break when the break statement executes. In the for loop, we have given a condition that when the i value becomes 5, the loop should be terminated. We used the if statement inside the for loop to execute the break keyword. When the value of i becomes 3, then the break statement will execute. We can see in the output that the only 0 1 2 values are printed, but as loop gets break at i=3, so 3 will not be printed.
- Catch: This keyword is used to catch a specific error that has occurred. The catch statement must be used with the throw statement.
Example:
// Program to demonstrate the use of a catch statement.
#include <iostream>
using namespace std;
int main() {
try {
int age ;
cin>>age;
if (age >= 18) {
cout << "Access granted - you are old enough.";
} else {
throw (age);
}
}
catch (int myNum) {
cout << "Access denied - You must be at least 18 years old.\n";
cout << "Age is: " << myNum;
}
return 0;
}
Output:

Explanation:
The above code is used to demonstrate the use of catch statements in C++. In the above code, we have declared a try block that will test the block of code for the errors. If the condition is not satisfied, the throw statement will execute, sending the signal to the catch statement to execute then the catch block statement will execute.
- Case: The case is a keyword used with the switch statement. In the switch statement, we write different cases to be performed when a specific value is reached, so to define those cases, we use the case keyword.
Example:
// Program to demonstrate the use of case statements.
#include <iostream>
using namespace std;
int main() {
char oper; float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}
return 0;
}
Output:

Explanation:
The above code demonstrates the use of case keywords in c++. We have built a calculator in the above code using the switch and case keywords. In the code, we have taken the operator from the user. We have given the operator as a parameter to the switch statement. When a particular operator is entered that is present in the case statement, then the statement in the specific case will be executed. In the output, we can see that we have given * as the operator. So now the case with the * operator will execute, which is multiplication.
- Char: The char is a data type that is given to a variable. The variable with char data type will take only one character as the input.
Example:
// Program to demonstrate the use of char statement.
#include <iostream>
using namespace std;
int main() {
//declaring a variable as a char data type
char a;
cin>>a;
cout<<a;
return 0;
}
Output:

Explanation:
The above code demonstrates the use of the char keyword in c++. We have declared a character type variable in the code using the char keyword. We have taken the input from the user and printed the output using the cout statement.
- Class: Class is a keyword used to declare a class in the C++ program.
Example:
// Program to demonstrate the use of the class statement.
#include <bits/stdc++.h>
using namespace std;
//Creating a class using the class keyword.
class student{
public:
//Creating a function in the class
void display(){
cout<<"Hi from student class";
}
};
int main(){
//creating an object for the class
student x;
//calling the display function using the object.
x.display();
return 0;
}
Output:

Explanation:
The above code is written to demonstrate the use of the class statement in c++. We created a class with a function display in the above code. We have created the class using the class keyword. In the main function, we created an object for the class and used the object. We have called the function member using objects.
- Const: The const is a keyword in C++. It defines a variable whose value will not change in the program throughout the execution.
Example:
// Program to demonstrate the use of const statement.
#include <bits/stdc++.h>
using namespace std;
int main(){
//creating a constant variable.
const int a;
//trying to change the value of the constant variable.
for(int i=0;i<5;i++){
a=a+1;
cout<<a;
}
}
Explanation:
The above code is used to demonstrate the use of the const keyword. In the above code, we have created a variable a and set it as a constant value. An error will occur when we try to change a value using the for loop. This is because the value a is set to a constant value using the const keyword. This keyword will not change the value throughout the program.
- Continue: This statement is used with the loop. When this statement is executed, the control is directly sent to the start of the loop without executing the statement below the continue statement.
// Program to demonstrate the use of the continue statement.
//To use the input and output statements, we need to use the iostream header files
#include <iostream>
using namespace std;
int main() {
int number;
int sum = 0;
// nested for loops
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
//If the j value will become 2, then the continue statement will execute.
continue;
}
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
Output:

Explanation:
The above code is written to demonstrate the continue keyword in c++. We used the nested loop in the above code to show how the continue statement works. In the second loop, when the j value becomes 2, the continue statement will execute and send the control to the start of the loop. Due to this, we can observe in the output that the j values will only be 1 and 3.
- Default: The default statement is used with the switch statement. The default statement is executed when the case statement doesn't match any switch condition.
Example:
// Program to demonstrate the use of default statements.
#include <iostream>
using namespace std;
int main() {
char oper; float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /) then default statement will execute.
cout << "Error! The operator is not correct";
break;
}
return 0;
}
Output:

Explanation:
The above code is written to demonstrate the default reserved keyword in c++. We have created a switch statement with different cases in the above code. We have also created a default statement. In the output, we have entered & operators, which do not match with any case statement, so the default statement will execute.
- Delete: Delete is a keyword in c++ that is used to deallocate the memory for an object.
Example:
// Program to demonstrate the delete statement.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// ptr is the NULL pointer
int* ptr = NULL;
// deleting pointer
delete ptr;
return 0;
}
Explanation:
The above code is written o demonstrate the use of the delete keyword in c++. We have created a pointer in the code and assigned null to it. Then we used the delete operator to delete the pointer.
- Double: Double is a data type in c++ used to define a floating point.
- Else: else is a statement in c++ used with an if statement. When the if statement condition fails, the else statement executes.
- Enum: The enum is a user-defined data type. The enum data type will have integral constants.
Example:
// Program to demonstrate the enum statement.
#include <iostream>
using namespace std;
//declaring the enum data type with some integral constants.
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}
Output:

Explanation:
The above example demonstrates the use of the enum keyword in c++. We have created an enum data type using the days. When we have printed today+1, 4 is printed because Wednesday's number is 3 in the enum. In the main function, we created an object for the enum and assigned one of the integral constants.
- Explicit: Explicit is a keyword in c++ which is used with the constructor. This keyword is used to prevent the constructor from implicit type conversion.
Example:
// C++ program to illustrate explicit conversions
#include <iostream>
using namespace std;
class Demo{
public:
//using explicit keywords before the constructor.
explicit Demo(int n){
demo1 = n;
}
int getDemo(){
return demo1;
}
private:
int demo1;
};
void getDemoExternally(Demo demo){
cout << demo.getDemo();
}
// Main function.
int main()
{
getDemoExternally(10);
return 0;
}
Explanation:
The above code demonstrates the use of explicit keywords in c++. In the code, we have created a class with the name demo. We have created a constructor in the class, and then we created a getdemo function member. Then we have created a new function with the name getDemoExternally. We have created a function member with the keyword explicit. The getdemoexternally function has class as a parameter. So, when we call the function, the integer data type is converted to the demo class, but this is in the case of the implicit type conversion. But here, we have used the explicit keyword before the constructor, so this is impossible. An error will occur when we run the code because we have called the getdemoexternally function from the main function with the integer parameter, but the constructor function is explicit.
- Float: float is a data type in c++ that takes decimal values.
- For: The for is a keyword in c++ that is used for the looping between the statements until the condition fails.
- Friend: The friend keyword is used to declare the friend function that can access all the private and protected variables in a class. To use the friend function, we need to declare the friend function in the class, and then we can define the function in any part of the code.
Example:
#include<iostream>
using namespace std;
class base {
int a, b;
public:
void get() {
cout << "Enter two values:";
cin >> a>>b;
}
friend float mean(base ob);
};
float mean(base ob) {
return float(ob.a + ob.b) / 2;
}
int main() {
base obj;
obj.get();
cout << "Mean value is : " << mean(obj);
}
Output:

Explanation:
The above code is used to demonstrate the use of the friend function keyword. We have declared a class base with two private variables in the code. We have also declared a friend function in the class to access the private variables. Outside the class, we have defined the friend function. In the main function, we have created the object for the class. Using the object, we have called the get function in the class, which is used to set the values of the variables. Then using the friend function, we have passed the class object as the parameters, which will return the means of the two values we entered using the get function.
- Int: The int is a keyword used to define a variable of integer type.
- Long: long is a keyword in c++ which is used to define a 32-bit integer type variable.
- Mutable: The mutable is a keyword in c++ that is used before a variable. Mutable is opposite to the constant. When a variable is defined with the mutable keyword, the value can be changed even during the runtime. The variable values can also be changed in the constant class.
Example:
#include <iostream>
using namespace std;
class X
{
public:
//creating a constant class.
bool Get() const
{
m_accessCount++;
return m_flag;
}
private:
bool m_flag;
//creating a mutable value m_accessCount
mutable int m_accessCount;
};
int main()
{
}
Explanation:
The above code is used to demonstrate the mutable keyword in c++. We have created two private variables in the code, one with the mutable keyword. We have also created a get() function defined with the const keyword. Even if the class is defined with the constant keyword, the value of the variable m_accessCount can be changed because we have declared the variable with a mutable keyword. When we remove the mutable keyword and run the code, then Error will occur.
- New: The new is a keyword in c++ used to allocate the memory for the object in the code.
Example:
#include <bits/stdc++.h>
using namespace std;
int main() {
// pointer initialized
int * a = new int(5);
//value printed
cout << * a << endl;
}
Output:

Explanation:
The above code is written to demonstrate the new operator. In the above code, we created an array with the help of the new operator and used the same operator. We inserted variable 5 into the array. Using the cout statement, we have printed the values in the array a.
- Private: Private is a variable used to declare the class members who are not visible outside the class
- Operator: In C++, an operator is overloaded with a new declaration with this method
- Protected: Protected is a keyword in c++ declared with the class members. When a class member is defined with the protected keyword, the members can be accessed only by the respective class and derived classes.
- Public: Public is a keyword in c++ which is used to define the class member. When a class is defined with the public keyword, the members can be accessed within any part of the code.
Example:
#include <iostream>
using namespace std;
class Base {
private:
int pv = 2;
protected:
int pr = 3;
public:
int pu = 9;
int get() {
return pv;
}
};
class PublicDerived : public Base {
public:
int getProt() {
return pr;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.get() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pu << endl;
return 0;
}
Output:

Explanation:
We have created private, protected, and public variables in the above code. As discussed, the private variable can only be accessed in the class. The protected variable can be accessed in the derived class. The public variable can be accessed in any part of the code. In the above code, we have used the get() function to get the private variable, whereas the protected variable is accessed from the derived class.
- Return: The return is a keyword in c++ that will return an object when some function is called.
- Short: short is a keyword in c++ used to define a variable of 16- bit integer type data type.
- Sizeof(): Thesizeof will return the size of the object, data type or the variable in bytes.
- Struct: Thestruct is used to define a user-defined datatype containing different data types.
Example:
#include <iostream>
using namespace std;
struct Person
{
int age;
string name;
};
int main(void) {
struct Person p;
p.age = 18;
p.name = "Example";
cout << "Person name: " << p.name << endl;
cout << "Person age: " << p.age << endl;
return 0;
}
Output:

Explanation:
The above code is used to demonstrate the use of the struct keyword in c++. In the code, we created a structure of user-defined data type using the struct keyword. In the user-defined data type, we have int and string data types. In the main function, we have created an object for the struct using the object and set the values of the variable in the struct person.
- Static: The static is a keyword in c++. When a variable or data member is defined with the static keyword, the data will be in static storage. The specific line where the static variable is declared with the value will be executed only once throughout the program.
Example:
#include <iostream>
using namespace std;
int main(void) {
for(int i=0;i<5;i++){
static int a=1;
a=a+1;
cout<<a;
}
}
Output:

Explanation:
The above code is used to demonstrate the static keyword in c++. In the for loop, we have declared the static variable with the value 1. So that declaration statement will be executed only once, so for every loop, a value will not change to 1.
- Switch: The switch statement compares the expression with the case values.
- This: This statement points to an object or an instance of the class created.
- Throw: throw keyword will send an exception when a specific problem is detected. The catch block will catch the error.
- True: True is a Boolean value that is equivalent to 1.
- Try: Try is a keyword in c++. When we declare the block with the try keyword, the code will be tested for error during execution.
- Typedef: typedef is a keyword in c++ which is used to give a new name to the existing datatype.
Example:
#include <iostream>
using namespace std;
typedef struct Person
{
int age;
string name;
} ob;
int main(void) {
ob p;
p.age = 18;
p.name = "Example";
cout << "Person name: " << p.name << endl;
cout << "Person age: " << p.age << endl;
return 0;
}
Output:

Explanation:
The above code demonstrates the use of the typedef keyword in c++. In the above code, we have declared a structure data type. Before defining the structure data type, we used the typedef keyword, which allows us the create another name for the structure, that is what we did at the end of the declaration. We have created a new name ob for the structure. In the main function, we have created the object using the ob name.
- Union: Union keyword is a keyword in c++ used to define the user-defined datatype. The main use of the union is that all the members in the union share the same memory location.
union Data {
int i;
float f;
} data, data1;
In this way, we have to define a union data type. If we change one value in the union then all the varaibles present in the union will have same value.
- Virtual: A virtual keyword along with the class member function. When a member function is defined with the virtual keyword inside the class, the derived class must define the virtual function.
- Void: The void keyword can be used in two ways. If the void keyword is used in the function parameter, the function will not take any parameter. When the void is defined with the function name, the function doesn't return any value.
- While: While is a loop statement. The statement inside the while block will execute until the while condition fails.