Introduction to C++ | Keywords

C++ Keywords

In C++, keywords are reserved words that have a special meaning to the compiler. They are part of the syntax of the C++ language and cannot be used as identifiers (e.g., variable names, function names). C++ has a set of predefined keywords that form the foundation of the language's structure.

Common C++ Keywords

C++ has many keywords, and here are some of the most commonly used:

1. Data Types
2. Control Flow
3. Functions
4. Object-Oriented Programming (OOP)
5. Memory Management

Code Example: Using Keywords in C++

Here's an example that demonstrates the use of various C++ keywords.

Code Example


                #include <iostream>
                using namespace std;
        
                // Function definition
                void displayMessage() {
                    cout << "Hello, this is a simple C++ program!" << endl;
                }
        
                int main() {
                    int num = 10;
                    bool isActive = true;
        
                    // Conditional statement using 'if' and 'else'
                    if (isActive) {
                        cout << "The program is active." << endl;
                    } else {
                        cout << "The program is inactive." << endl;
                    }
        
                    // Calling the function 'displayMessage'
                    displayMessage();
        
                    // Returning from the function
                    return 0;
                }
                    

Output

The program is active.
Hello, this is a simple C++ program!

Pro Tip:

💡 Pro Tip

When using C++ keywords, remember that they are reserved by the compiler and cannot be used as identifiers for variables or functions. Keywords are fundamental to the syntax and structure of a C++ program, so it is essential to familiarize yourself with them early on to write effective code.