C++ Keywords - C++ Programming Tutorial
C++ Course / C++ Basics / C++ Keywords

C++ Keywords

BLUF: Mastering C++ Keywords is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Keywords

C++ is renowned for its efficiency. Learn how C++ Keywords enables low-level control and high-performance computing in the tutorial below.

C++ reserved terms are fundamental in establishing the structure and behavior of the language. These keywords encompass predefined terms that serve various purposes, such as declaring variable types, controlling program execution, and enabling extra functionalities. Proficiency in these concepts is vital for proficient C++ development and empowers developers to create robust and flexible software solutions.

A reserved word in the C++ programming language, a keyword holds specific meanings and cannot be employed as a variable name or constant name. These reserved words play a crucial role in defining various elements of the C++ language such as void, int, public, return, and more.

C++ Keywords Example

Let's consider a scenario to demonstrate the application of keywords in C++.

Example

Example

#include<iostream>

using namespace std; //using namespace keyword

int main() //using int keyword

{

	cout<< "Hello! Welcome to the Cpp Tutorial World";

return 0; //using return keyword

}

Output:

Output

Hello! Welcome to the Cpp Tutorial World

Explanation:

In this instance, we are employing various terms such as utilizing, namespace, and returning, to perform certain operations within the C++ programming language. Within C++, these terms are predefined and consistently maintain their intended significance.

Keywords in C++

C++ programming language brings forth a variety of keywords in its latest version. Below is a table listing 32 Keywords in C++ Language, which are also present in the C language.

auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while

A list of 30 Keywords in C++ Language that are not available in C language is given below.

asm dynamic_cast namespace reinterpret_cast bool
explicit new static_cast false catch
operator template friend private class
this inline public throw const_cast
delete mutable protected true try
typeid typename using virtual wchar_t

Keywords Available in Both C and C++

Let's explore each of these C++ reserved words individually along with their explanations.

Keyword Name Description
auto In C++, the auto keyword is mainly used for type inference. It enables the compiler to determine a variable's data type from its initializer expression.
break By using the break keyword, the execution of a loop or switch statement is stopped.
case It is used in a switch statement to provide several scenarios in which different code blocks should be run depending on the result of an expression.
char The char keyword is used to declare character variables or data types.
const Constant values or pointers that refer to constants are declared with the keyword const.
continue This keyword is used to go on to the next iteration of a loop without running the last remaining statements.
default When no other case matches, it is used in a switch statement to provide a default case.
do This keyword initiates a loop that runs a series of statements as long as a certain condition is met.
double Double-precision floating-point variables and data types are declared with the double keyword.
else It works in conjunction with the if statement to specify an alternative code snippet that is run if the condition is false.
enum An enumeration is defined by the enum keyword. A user-defined type is made up of a collection of named integer constants.
extern It is used to declare variables or functions that are declared in another source file.
float The float keyword is used to declare single-precision floating-point variables or data types.
for It begins a loop by introducing initialization, condition, and update expressions.
goto The goto keyword is used to give an unconditional jump from the goto to a labeled statement within the same function.
if This operator is used to run a block of code only if a certain condition is met.
int The int keyword is used to declare the integer variables or data types.
long The long keyword is utilized to declare the integer variables with a wider range than int.
register The compiler is advised to put the variable in a register for quicker access.
return This keyword is used to end a function and, if desired, to provide the caller a value back.
short The short keyword is used to declare an integer short variables.
signed It is used to declare variables using signed integers.
sizeof The sizeof() keyword is used to calculate the size (in bytes) of a data type or object.
static The static keyword is used to declare static variables, which keep their values across function calls.
struct A composite data type with a single name for all its variables is defined with the struct keyword.
switch It is used to construct a switch statement, which compares an expression to its value and chooses a code block accordingly.
typedef The typedef keyword is used to give a data type an alias (alternative name).
union It is used to define a union, which resembles a struct but has one shared memory location for all its members.
unsigned The unsigned keyword is used to define unsigned integer variables.
volatile This keyword instructs the compiler that a variable's value may change at any point, even if it is not immediately apparent from the logic of the program.
void This keyword is used to indicate a function that does not return a value. It also determine a pointer to a data with an unspecified type.
while This keyword introduces a loop that repeatedly runs a set of statements if a certain condition is met.

Additional C++ Keywords Not Available in C

Let's explore a few extra C++ keywords that do not exist in C++. Here are a few examples:

Keyword Name Description
asm The asm keyword is used to write programs in assembly language that can be inserted into C++ programs. It enables the writing of inline assembly code by programmers for certain hardware operations.
dynamic_cast In C++, dynamic type casting uses this keyword. In object-oriented programming, it is mostly used for secure downcasting (from a base class to a derived class) when polymorphism is present.
namespace The namespace keyword is used to construct a named scope, which aids in organizing and bringing together similar code pieces. It is very helpful in avoiding name disputes.
reinterpret_cast In C++, this keyword is used to type casting at the lowest level. Without altering the actual data, it may be used to change the type of a pointer to a different type.
bool The bool keyword is used to define the boolean data type that can only store true or false values. It is frequently employed in conditional statements and logical procedures.
explicit In C++, the explicit keyword is used to specify that a constructor shouldn't be used automatically to convert types. It reduces the likelihood of accidental automated type conversions.
new The heap memory is used to dynamically allocate memory for objects during runtime. It gives back a pointer to the RAM that was allotted.
static_cast In C++, this keyword is utilized for fundamental type casting. It may be used to safely convert between kinds of related types.
false The boolean value false is represented by the term false. One of C++'s two boolean literals, the other being true.
catch The catch keyword in exception handling is used to capture and manage errors that the try block throws. It designates a section of code to run in response to a certain exception being thrown.
operator In C++, standard operators can be overloaded to operate with user-defined types by using the operator keyword. It permits the development of unique operator behavior.
template By parameterizing types and functions, templates are defined by the template keyword, which enables the generic programming. Templates provide for flexibility and reuse of code.
friend A class or function is designated as a friend of another class using the friend keyword. Friends have access to their classmate's private information and protected information.
private The access level of class members is specified using the private keyword. Only the class itself has access to private members.
class In C++, a class is defined with the class keyword. A user-defined data type called a class that contains both data and functions.
this It is a reference to the current instance of a class that is referred to by the term. Within its member functions, it is utilized to access members of the class.
inline The inline keyword is used to instruct the compiler to expand a function inline. As a result, the performance can be increased by lowering function call overhead.
public The access level of class members is specified with the public keyword. Access to public members is available during the whole program.
throw The throw keyword is used to manually throw an exception in C++. It is a crucial component of the C++ exception-handling system.
const_cast This keyword is used to remove a variable's const-ness. It mostly serves to change a variable's type qualifiers.
delete The memory that was previously allocated using the new keyword is released with the delete keyword. Memory leaks must be avoided at all costs.
mutable In C++, the mutable keyword is used to denote the ability to change a member of a const object. It is frequently employed when member variables within const member functions need to be updated.
protected The protected keyword is used to specify the access level of class members. Accessible inside the class and classes descended from it are protected members.
truth The word "true" stands for the boolean value "true". Along with false, it is one of the two boolean literals in C++.
try In exception handling, the try keyword is used to surround a block of code that may throw exceptions. One or more catch blocks are placed after it to deal with certain exceptions.
typeid The typeid keyword is used to learn more about an expression's type. It is frequently employed for runtime type identification.
typename In templates, we may use the typename keyword to indicate that a dependent name is a type. When dealing with template metaprogramming, it is frequently employed.
using Namespace aliasing, template specialization, and declarations all make use of the keyword. It facilitates namespace management and increases code readability.
virtual The terms inheritance and polymorphism are used together to describe the concept of virtual. It designates a member function as virtual so that descendant classes may override it.
Wchar_t A wide character data type that can store expanded character sets and support internationalization is defined by the wchar_t keyword.

Conclusion

In summary, for any developer aiming to harness the complete capabilities of this versatile coding language, understanding the significance and usage of keywords in C++ is essential. These keywords encompass a diverse array of functionalities such as managing memory, declaring types, controlling program flow, and manipulating data.

The distinctive keywords in the C++ programming language play a crucial role by introducing advanced functionalities and concepts that distinguish C++ from its predecessor, C. These keywords expand the capabilities of C++ by enabling developers to delve into concepts such as object-oriented programming, templates, exception handling, and type conversion. By leveraging terms like "namespace," "operator," "template," and "dynamic_cast," developers can craft modular, efficient, and eloquent codebases.

In C++, reserved words act as the essential elements that enable programmers to transform conceptual concepts into functional code. They provide developers with the means to manipulate data, control program execution, and create higher-level representations, ultimately enhancing the efficiency and organization of software development.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience