C++ Keywords



In C++, keywords are reserved words that have special meanings to the compiler. They cannot be used for any other purpose or as identifiers, such as variables or function names. It’s a predefined words that are part of the C++ syntax. They help define the structure and behavior of the code.

Basic Data Type keywords

These keywords define basic data types βˆ’

  • int βˆ’ integer data type.
  • char βˆ’ character data type.
  • float βˆ’ single-precision floating-point data type.
  • double βˆ’ double-precision floating-point data type.
  • void βˆ’ indicates no value or type; commonly used for functions that do not return a value.
  • Bool βˆ’ boolean data type (true or false).
  • wchar_t βˆ’ Represents wide character type mainly useful for internationalization.

Control Flow Keywords

Control flow keywords are special reserved words in programming that are used for implementing decision-making and repetitive tasks in programming.

  • Conditional Statements βˆ’ if, else, switch, case, default
  • Looping Constructs βˆ’ for, while, do
  • Control Statements βˆ’ break, continue, return, goto

Storage Class Keywords

These keywords specify the storage duration and linkage of variables βˆ’

  • auto βˆ’ The compiler automatically deduces the variable's type (in C++11 and later).
  • Register βˆ’ It suggests that the variable should be stored in a CPU register for faster access.
  • Static βˆ’ It indicates that the variable retains its value even after the scope in which it was defined ends.
  • extern βˆ’ It declares a variable that is defined in another translation unit.
  • Mutable βˆ’ It allows a member of a class to be modified even if the object is constant.

Modifiers

These keywords are used in modifying the properties of data types βˆ’

  • const βˆ’ It indicates that a variable's value cannot be changed after initialization.
  • volatile βˆ’ It indicates that a variable's value may change unexpectedly, preventing certain compiler optimizations.
  • signed βˆ’ It indicates that a data type can hold both positive and negative values.
  • unsigned βˆ’ It indicates that a data type can only hold non-negative values.
  • short βˆ’ It indicates a shorter version of the integer type.
  • long βˆ’ It indicates a longer version of the integer type.

Function Keywords

These keywords define specific behavior for functions

  • inline βˆ’ Suggests to the compiler to attempt to expand the function inline, reducing the overhead of a function call.
  • virtual βˆ’ Indicates that a function can be overridden in derived classes.
  • explicit βˆ’ Prevents implicit conversions for constructors or conversion operators.

Class and Object Keywords

These keywords are fundamental concepts in object-oriented programming (OOP) that enable developers to define and manipulate user-defined data types.

  • Class Definitions βˆ’ class, struct, union, enum
  • Namespace Management βˆ’ namespace, this
  • Memory Management βˆ’ new, delete

Access Specifiers

Access specifiers are keywords in object-oriented programming that define the accessibility or visibility of class members (attributes and methods) to other parts of a program.

  • public
  • protected
  • private

Exception Handling Keywords

These keywords are used for handling exceptions

  • try βˆ’ It defines a block of code to be tested for exceptions.
  • catch βˆ’ It defines a block of code that handles exceptions thrown by a corresponding try.
  • throw βˆ’ Used to signal the occurrence of an exception.

Operator Keywords

Operator keywords are keywords that allow you to define or change how operators (like +, -, *, etc.) work with custom data types, such as classes.

  • sizeof
  • typeid
  • alignof
  • alignas

Namespace Keywords

These keywords manage the scope of identifiers

  • namespace βˆ’ Defines a scope that can contain identifiers to avoid name collisions.
  • using βˆ’ Allows the use of names from a namespace without qualification.

Type Casting Keywords

These keywords are used for explicit type conversions

  • static_cast βˆ’ It performs a compile-time type check and conversion.
  • dynamic_cast βˆ’ Safely converts pointers or references within an inheritance hierarchy (requires RTTI).
  • const_cast βˆ’ It adds or removes const or volatile qualifiers.
  • reinterpret_cast βˆ’ It converts any pointer type to any other pointer type with no safety checks.

Miscellaneous Keywords

Some other keywords provided by the C++ library, that serve various purposes beyond the core functionalities of data types, control flow, or object-oriented programming.

  • using, typedef
  • Type Traits βˆ’ decltype, static_assert
  • Casting Operators βˆ’ static_cast, dynamic_cast, const_cast, reinterpret_cast

Keywords Vs. Identifiers

Keywords are predefined and reserved by the programming language, it has specific functions and meanings in the language while identifiers are user-defined names for program elements, They are created to represent variables, functions, and other entities in the code.

Example

Keywords Identifiers
int, float, while, public, private, class, return, etc. myVariable, calculateSum, Person, _tempValue, etc.
Advertisements