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 −

  • intinteger data type.
  • charcharacter 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.
  • Boolboolean 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