C++ Keywords
Keywords in C++ are reserved words that have special meaning in the language syntax. They cannot be used as identifiers (like variable names, function names, or class names). Each keyword serves a specific purpose in defining the structure and functionality of a C++ program. Here's an overview of the keywords in C++, along with their categories and explanations.
1. Data Types Keywords
These keywords define the types of data that can be used in a program.
int
: Represents integer data type.char
: Represents character data type.float
: Represents floating-point data type (single precision).double
: Represents double precision floating-point data type.void
: Indicates that a function does not return a value.
2. Control Flow Keywords
These keywords control the flow of the program.
if
: Executes a block of code if a specified condition is true.else
: Executes a block of code if the correspondingif
condition is false.else if
: Used to specify a new condition to test if the previous conditions were false.switch
: Allows a variable to be tested for equality against a list of values (cases).case
: Defines a branch in aswitch
statement.default
: Specifies the code to execute if no case matches in aswitch
statement.for
: Used for looping; repeats a block of code a specified number of times.while
: Repeats a block of code while a specified condition is true.do
: Similar towhile
, but guarantees at least one execution of the loop.break
: Exits from a loop or switch statement.continue
: Skips the current iteration of a loop and continues with the next iteration.return
: Exits from a function and optionally returns a value.
3. Storage Class Keywords
These keywords define the storage duration and visibility of variables.
auto
: Automatically determines the variable type (in C++11 and later).register
: Suggests that a variable should be stored in a CPU register for faster access (note that this is often ignored by modern compilers).static
: Preserves the value of a variable across multiple function calls.extern
: Indicates that a variable is defined in another file or scope.mutable
: Allows a member of a class to be modified even if the class instance is constant.
4. Type Keywords
These keywords define user-defined types.
class
: Defines a class, which is a blueprint for creating objects.struct
: Defines a structure, similar to a class but with public members by default.union
: Defines a union, which allows storing different data types in the same memory location.enum
: Defines an enumeration, a user-defined type consisting of named integral constants.typedef
: Creates an alias for an existing data type.
5. Function Keywords
These keywords relate to function definitions and declarations.
inline
: Suggests to the compiler to expand the function inline (replace the function call with the function code) for optimization.virtual
: Allows a member function to be overridden in derived classes, enabling polymorphism.friend
: Grants access to the private and protected members of a class to a specified function or another class.
6. Exception Handling Keywords
These keywords are used for handling exceptions.
try
: Defines a block of code to be tested for exceptions.catch
: Defines a block of code to handle exceptions thrown by thetry
block.throw
: Used to throw an exception.
7. Namespace Keywords
These keywords help in organizing code into logical groups and preventing name clashes.
namespace
: Declares a namespace, which is a declarative region that provides a scope to the identifiers inside it.using
: Allows the use of identifiers from a namespace without qualifying them with the namespace name.
8. Other Keywords
These keywords serve various purposes.
const
: Indicates that a variable's value cannot be modified after initialization.volatile
: Tells the compiler that a variable's value may change at any time (often used in multi-threaded programs).sizeof
: Returns the size (in bytes) of a data type or object.alignas
: Specifies the alignment requirement for a variable.alignof
: Returns the alignment of a type.noexcept
: Indicates whether a function is allowed to throw exceptions.decltype
: Inspects the declared type of an expression.constexpr
: Indicates that the value of a variable or function can be evaluated at compile time.