Back to C++
2026-04-136 min read

Keywords (C++)

Learn Keywords (C++) step by step with clear examples and exercises.

Title: Mastering C++ Keywords: A full guide for Aspiring Programmers

Why This Matters

In C++, keywords play a crucial role in defining the structure of your code and controlling its flow. Understanding these keywords is essential to writing efficient, error-free programs. This knowledge will not only help you pass exams and interviews but also save you from real-world programming bugs.

Prerequisites

Before diving into C++ keywords, it's important that you have a solid understanding of the following:

  1. Basic C++ syntax
  2. Data types and variables
  3. Operators in C++
  4. Control structures (if-else, loops)
  5. Functions and their basics
  6. Input/Output operations

Core Concept

C++ keywords are reserved words that have special meanings within the language. They cannot be used as variable names or function names without causing syntax errors. This section will cover some of the most important C++ keywords, along with brief explanations and examples.

  1. auto: Automatic data type deduction for variables
  2. break: Exits a loop prematurely
  3. case: Used in switch statements to specify different actions for different cases
  4. catch: Handles exceptions thrown by try blocks
  5. char: Character data type
  6. class: Defines a user-defined data type (object)
  7. const: Declares a constant variable that cannot be changed
  8. constexpr: A function or variable that can be evaluated at compile time
  9. continue: Skips the current iteration of a loop and moves to the next one
  10. default: The default action when no case matches in a switch statement
  11. delete: Deletes a function or operator from its class
  12. do...while: A loop that executes at least once before checking the condition
  13. double: Floating-point data type with double precision
  14. dynamic_cast: Safely cast objects between related classes
  15. else: The alternative action when a condition is false
  16. enum: Defines an enumerated data type (enumeration)
  17. explicit: Prevents implicit conversions for constructors and user-defined literals
  18. export: Allows symbols to be visible outside the current translation unit
  19. false: Boolean literal representing false
  20. final: Prevents further derivation from a class or overriding of a function
  21. float: Floating-point data type with single precision
  22. for: A loop construct for iterating a fixed number of times
  23. friend: Declares a non-member function as a friend of a class
  24. goto: Unconditional jump to a labeled statement
  25. if: Conditionally executes code based on a condition
  26. inline: Hints the compiler to inline a function call
  27. int: Integer data type
  28. long: Long integer data type (32-bit or 64-bit, depending on platform)
  29. namespace: Organizes code into logical units called namespaces
  30. new: Dynamically allocates memory for objects
  31. operator: Defines a user-defined operator for a class
  32. private: Access modifier restricting access to members within a class
  33. protected: A hybrid access modifier providing partial access control
  34. public: Default access modifier granting unrestricted access to members
  35. register: Suggests the compiler to keep a frequently used variable in a CPU register
  36. reinterpret_cast: Performs type punning (converting between unrelated types)
  37. return: Exits a function and returns a value (optional for main functions)
  38. short: Short integer data type (16-bit, depending on platform)
  39. signed: Specifies that an integer can represent negative values
  40. sizeof: Determines the size of a data type or object in bytes
  41. static: Static storage class specifier for variables and functions
  42. static_cast: Performs a static cast (converting between related types)
  43. struct: Defines a user-defined data type (structure)
  44. switch: A multiway branching construct based on the value of an expression
  45. template: Declares a template for generic functions and classes
  46. this: Pointer to the current object in non-static member functions
  47. throw: Throws an exception from a function or block
  48. true: Boolean literal representing true
  49. try: Encloses code that may throw exceptions and handles them using catch blocks
  50. typeid: Returns a type_info object representing the dynamic type of an expression
  51. typedef: Defines a new name (alias) for an existing data type
  52. union: Defines a user-defined data type (union) that can store different data types in the same memory location
  53. unsigned: Specifies that an integer cannot represent negative values
  54. using: Declares a new name (alias) for a class, function, or namespace member
  55. virtual: Declares a virtual function that can be overridden by derived classes
  56. void: Empty data type used for functions without return values
  57. volatile: Specifies that the value of a variable may change unexpectedly (e.g., due to hardware interaction)
  58. wchar_t: Wide character data type
  59. while: A loop construct for repeatedly executing code as long as a condition is true
  60. xor: Bitwise exclusive OR operator

Worked Example

Let's take a look at a simple example using some of the keywords mentioned above:

#include <iostream>
using namespace std;

struct Person {
string name;
int age;
};

void printPerson(const Person& person) {
cout << "Name: " << person.name << ", Age: " << person.age << endl;
}

int main() {
Person john = {"John", 30};
printPerson(john);
return 0;
}

In this example, we define a Person struct and a function called printPerson that takes a const reference to a Person. In the main function, we create an instance of Person named john, call the printPerson function with john, and return 0 to indicate successful execution.

Common Mistakes

  1. Forgetting semicolons at the end of statements: This can lead to syntax errors.
  2. Misusing keywords as variable names or function names: Using reserved words as identifiers will cause compile-time errors.
  3. Ignoring the difference between = and ==: Assignment (=) is used to assign values, while comparison (==) is used to compare values.
  4. Not understanding the scope of variables: Variables declared within a block or function have local scope, while those declared outside functions have global scope.
  5. Overusing global variables: Global variables can lead to unintended side effects and make code harder to maintain.

Practice Questions

  1. Write a program that uses the for loop to print numbers from 1 to 10.
  2. Define a class called Rectangle with private data members for the length and width, and public member functions to calculate the area and perimeter.
  3. Write a function called swap that takes two integers as arguments and swaps their values without using a temporary variable.
  4. Implement a simple calculator program that performs addition, subtraction, multiplication, and division using user input.
  5. Write a program that uses the switch statement to implement a basic command-line interface for a simple calculator.

FAQ

What is the difference between int and integer?

In C++, int is a specific data type representing an integer, while "integer" refers generally to any whole number.

Can I redefine keywords in my code?

No, you cannot redefine reserved keywords as identifiers in your code.

What happens if I use a keyword as a variable name?

Using a keyword as a variable name will cause a compile-time error.

Is it possible to override the operator+ for a custom data type?

Yes, you can override operators like operator+ for user-defined data types using the operator keyword.

What is the purpose of the static keyword in C++?

The static keyword has several uses in C++, including declaring static variables and functions, ensuring that a variable maintains its value between function calls, and preventing a variable from being allocated on the stack each time a function is called.

Keywords (C++) | C++ | XQA Learn