Welcome to The Coding College! In this tutorial, we’ll dive into C++ keywords—the reserved words in C++ that have predefined meanings and cannot be used as identifiers (like variable names). Understanding keywords is crucial for writing syntactically correct C++ programs.
What Are Keywords in C++?
Keywords are the foundation of the C++ language. Each keyword is reserved by the compiler to perform a specific task, such as declaring variables, controlling program flow, or defining classes and functions.
- Keywords are case-sensitive.
- You cannot use them as names for variables, functions, or any other user-defined elements.
Categories of Keywords
C++ keywords can be grouped based on their purpose:
- Data Types
- Control Statements
- Modifiers
- Access Specifiers
- Error Handling
- Others
Let’s explore these categories in detail.
1. Data Type Keywords
These keywords define the type of data a variable can hold.
Keyword | Purpose | Example |
---|---|---|
int | Integer data | int x = 10; |
float | Floating-point data | float y = 5.5; |
double | Double-precision floating-point | double z = 10.99; |
char | Character data | char c = 'A'; |
bool | Boolean data | bool flag = true; |
void | No return type | void display(); |
wchar_t | Wide character | wchar_t w = L'A'; |
2. Control Statements Keywords
These keywords control the flow of a program.
Keyword | Purpose | Example |
---|---|---|
if | Conditional statement | if (x > 0) { ... } |
else | Alternative path | else { ... } |
switch | Multiple conditions | switch (x) { ... } |
case | A single condition in switch | case 1: ... break; |
default | Default case in switch | default: ... |
for | Loop with a counter | for (int i = 0; ...) |
while | Loop with a condition | while (x < 10) { ... } |
do | Executes before condition check | do { ... } while(); |
break | Exit a loop or switch | break; |
continue | Skip iteration in loop | continue; |
return | Exit function and return value | return 0; |
goto | Jump to a labeled statement | goto label; |
3. Modifiers Keywords
These keywords modify data types to refine their behavior.
Keyword | Purpose | Example |
---|---|---|
signed | Signed integer | signed int x; |
unsigned | Unsigned integer | unsigned int y; |
short | Short integer | short int z; |
long | Long integer | long int a; |
const | Constant value | const int b = 10; |
volatile | Prevent compiler optimizations | volatile int x; |
static | Persistent value in a function | static int c; |
4. Access Specifiers Keywords
These keywords define access levels in classes and structures.
Keyword | Purpose | Example |
---|---|---|
public | Members accessible to all | public: int x; |
private | Members accessible within class | private: int y; |
protected | Members accessible in subclasses | protected: int z; |
5. Error Handling Keywords
These keywords handle exceptions in C++ programs.
Keyword | Purpose | Example |
---|---|---|
try | Begin exception handling block | try { ... } |
catch | Handle an exception | catch (...) { ... } |
throw | Throw an exception | throw "error"; |
6. Other Keywords
These keywords have specialized uses in C++.
Keyword | Purpose | Example |
---|---|---|
class | Define a class | class MyClass { ... } |
struct | Define a structure | struct MyStruct { ... } |
enum | Define an enumeration | enum Color { Red }; |
namespace | Avoid name conflicts | namespace MySpace { ... } |
using | Use a namespace | using namespace std; |
inline | Suggest inline function | inline void func(); |
typedef | Define a new type name | typedef int Age; |
nullptr | Null pointer constant | int* ptr = nullptr; |
operator | Overload an operator | int operator+(...); |
Examples Using Keywords
Example 1: Simple Program
#include <iostream>
using namespace std;
int main() {
int num = 10; // Declare a variable
if (num > 5) { // Use if condition
cout << "Number is greater than 5" << endl;
}
return 0;
}
Example 2: Class Declaration
#include <iostream>
using namespace std;
class MyClass {
private:
int x;
public:
void setValue(int value) {
x = value;
}
int getValue() {
return x;
}
};
int main() {
MyClass obj;
obj.setValue(42);
cout << "Value: " << obj.getValue() << endl;
return 0;
}
Reserved Identifiers in C++
In addition to keywords, C++ reserves some identifiers for internal use, such as names starting with underscores (_
or __
). Avoid using such identifiers to prevent conflicts.
Summary
- C++ has a rich set of keywords that are essential for programming.
- Understanding their purpose and usage helps in writing robust and error-free programs.
- Avoid using keywords as variable or function names.
Explore More at The Coding College
Visit The Coding College for a deeper dive into C++ programming, practical examples, and advanced techniques.