C++ Keywords
C++ keywords are reserved words that have special meaning in the C++ programming language. They are part of the syntax and cannot be used as identifiers (such as variable names, function names, etc.). Understanding these keywords is essential for writing valid C++ code.
Categories of Keywords
List of C++ Keywords
| Keyword | Explanation |
|---|---|
| int | Represents integer type. |
| float | Represents floating-point type. |
| double | Represents double-precision floating-point type. |
| char | Represents character type. |
| bool | Represents boolean type (true/false). |
| if | Used for conditional statements. |
| else | Used for alternative conditional statements. |
| switch | Used for multi-way branching. |
| case | Defines a branch in a switch statement. |
| default | Defines the default branch in a switch statement. |
| for | Used for for-loop iteration. |
| while | Used for while-loop iteration. |
| do | Used for do-while loop iteration. |
| break | Exits from a loop or switch statement. |
| continue | Skips the current iteration of a loop. |
| return | Exits from a function and optionally returns a value. |
| auto | Automatic storage duration (default storage class). |
| register | Suggests that a variable be stored in a register for faster access. |
| static | Extends the lifetime of a variable to the duration of the program. |
| extern | Declares a variable defined elsewhere (in another file or scope). |
| void | Indicates that a function does not return a value. |
| inline | Suggests that the compiler replace a function call with the function code. |
| virtual | Indicates that a function can be overridden in derived classes. |
| friend | Allows a function or class to access private members of another class. |
| try | Defines a block of code to be tested for exceptions. |
| catch | Defines a block of code to handle exceptions. |
| throw | Used to throw an exception. |
| class | Defines a class. |
| struct | Defines a structure. |
| namespace | Defines a scope for identifiers to avoid name conflicts. |
| template | Used to define a template for generic programming. |
| this | Refers to the current object in a class. |
| new | Allocates memory dynamically. |
| delete | Deallocates memory that was previously allocated with new. |
| sizeof | Returns the size of a data type or object. |
| typeid | Used to get the type information of an expression. |
| volatile | Indicates that a variable may be changed by external factors. |
| const | Indicates that a variable's value cannot be changed after initialization. |
Key Takeaways
- C++ keywords are reserved words that form the syntax of the language.
- Understanding keywords is crucial for writing correct and efficient C++ code.
- Keywords are categorized based on their functionality, such as data types, control flow, and storage classes.
- Familiarity with keywords enhances code readability and maintainability.