C++ Keywords and Identifiers

C++ Keywords

C++ keywords are the reserved words which are pre-defined in C++ programming language. We cannot use keywords as an identifier. Each keyword has a pre-defined meaning for C++ compiler. These words are the building blocks of the language, each serving a specific purpose in the syntax and structure of C++ programs.

  • Trying to use a keyword as an identifier will generate a compilation error.
  • All keywords are in lower case letters.

Types of Keywords

  • Data Type Keywords : These keywords are used to define the data type of variables. Examples include int, double, and char.

  • Control Structure Keywords : Keywords like if, else, while, and for are used to control the flow of a program through conditional statements and loops.

  • Storage Class Keywords : C++ provides keywords like static, extern, and auto to define the storage class of variables.

  • Function-related Keywords : Keywords such as void, return, and class are used to define functions and classes.

  • Namespace Keywords : C++ supports the concept of namespaces, and keywords like namespace are used for this purpose.
Here is the list of reserved keywords in C++.
asm else new this
auto enum operator throw
bool explicit private true
break export protected try
case extern public typedef
catch false register typeid
char floar reinterpret_cast typename
class for return union
const friend short unsigned
const_cast goto signed using
continue if sizeof virtual
default inline static void
delete int static_cast volatile
do long struct wchar_t
double mutable switch while
dynamic_cast namespace template

C++ Identifiers

Identifiers, on the other hand, are names given to various program elements such as variables, functions, arrays, etc. These names help us reference and manipulate these elements within our code. Unlike keywords, identifiers are user-defined, offering flexibility and descriptive power to the programmer.

Let's explore the rules and conventions for creating meaningful and effective identifiers:
  • Rules for Identifiers :
    • Must begin with a letter (uppercase or lowercase) or an underscore _.
    • Subsequent characters can be letters, digits, or underscores.
    • C++ is case-sensitive, so number and Number are treated as different identifiers.
      int myVariable = 42;
      double _pi = 3.141;
      char firstName = 'John';

  • Avoiding Keywords as Identifiers :
    • Never use C++ keywords as identifiers.
    • Doing so can lead to syntax errors and confusion.
    • // Incorrect usage
      int int = 5; // 'int' used as an identifier
      In this example, using int as a variable name is incorrect since it's a reserved keyword.

  • Choosing Descriptive Names :
    • Opt for names that clearly express the purpose or role of the variable or function.
    • Avoid overly short names (e.g., x, y) unless the context is clear.
    • // Less descriptive
      int x = 10;
      // More descriptive
      int numberOfStudents = 10;
      The more descriptive the identifier, the easier it is to understand the code.

Common Mistakes and Best Practices

As you navigate the world of C++, here are some common mistakes related to keywords and identifiers, along with best practices to steer clear of them:
  • Common Mistake: Using Underscores in Identifiers :
    • Avoid starting identifiers with a double underscore (__) or a single underscore followed by an uppercase letter (_X).
    • These are reserved for compiler-specific or system-specific purposes.
    • // Incorrect usage
      int __total = 50;
      double _Total = 75.5;

  • Common Mistake: Using Keywords as Identifiers :
    • Be vigilant about avoiding C++ keywords as identifiers, as this can lead to compiler errors.
    • // Incorrect usage
      int class = 3; // 'class' used as an identifier

  • Best Practice: Choose Meaningful Names :
    • Prioritize clarity over brevity when naming variables, functions, etc.
    • A more extended but descriptive name is often better than a short, ambiguous one.
    • // Better practice
      double calculateTotalCost(int price, int quantity) {
          // code implementation
      // Less descriptive
      double calc(int p, int q) {
          // code implementation

Here are few examples of valid and invalid Identifiers.

Valid Identifiers Invalid Identifiers
hello 1hello
Hello Hel/lo
_hello hel lo
hello_world hello,,,world


Congratulations! You've now gained insights into the essential concepts of keywords and identifiers in C++. These elements are the bedrock of your programs, shaping the way you express logic and interact with your code. Understanding C++ keywords and identifiers is crucial for writing clean, readable, and error-free code. By following naming conventions and choosing appropriate identifiers, you contribute to the clarity and maintainability of your code. As you continue your journey in C++, remember the importance of meaningful identifiers and the role of keywords in providing the structure of the language.

Feel free to experiment with different keywords, create expressive identifiers, and explore the vast landscape of C++ programming. Each keyword and identifier is a tool in your programming toolkit, enabling you to craft elegant and powerful solutions to real-world problems.