Keywords in C++
Learn via video course
Overview
Keywords in C++ are the collection of reserved words. These are written in lower cases and have a special meaning defined by the compiler. There are 95 keywords in C++, of which around 30 are unavailable in the C language. Keywords are always used for a special purpose in a program, but we can't use them as variable or function names. Some examples of Keywords in C++ are break, int, void, public, private, auto, return and many more.
Scope
- This article defines different types of Keywords present in C++ and how they differ from C++ Identifiers.
- We also learn about types of qualifiers in C++ using examples and their syntax. For better understanding, we illustrate all these using C++ programs.
Tokens in C++
Like living cells in the human body are the smallest possible units of life, we have tokens in C++, which are the smallest building blocks of a program.
Tokens in C++ can be classified as:
- Keywords
- Identifiers
- Constants
- Operators
- Strings
In this article, we will primarily learn about keywords, their types, and syntax and compare them with C++ Identifiers.
Keywords in C++
Keywords in C++ are the collection of reserved words. These are written in lower cases and have a special meaning defined by the compiler. We can’t use them to declare variable names or function names.
Example:
We can declare a variable 'nums' with any value, but we can't use keywords for this purpose.
Different types of Keywords in C++
There are a total of 95 Keywords in C++. Some of them are defined below:
auto | bool | break | case | catch | char | class |
const | continue | double | default | delete | else | enum |
explicit | friend | float | for | int | long | mutable |
new | operator | private | protected | public | register | return |
struct | switch | short | sizeof | static | this | typedef |
throw | true | try | union | virtual | void | while |
- auto - It is used to deduce the data type of an expression, and in general, we don't need to specify the data type. Still, we can commonly use the auto keyword, which will automatically initialize the variable accordingly.
- bool - It is used to define the data type and nature of the object, whether true/false.
- break - It terminates a switch statement or any loop.
- case - It is explicitly used for switch statements to support different situational cases.
- catch - It specifies actions to be taken when an exception occurs during the program compilation.
- char - It is used to define character objects.
- class - It is used to declare a user-defined structure that encapsulates data members or functions.
- const - It defines objects whose value will not change throughout program execution.
- continue - It transfers control to the start of a loop.
- default - It handles expression values in a switch statement that are not handled by the cases.
- delete - It is used to deallocate the memory of an object.
- double - It is used to define floating-point numbers.
- else - It is used mainly in if-else program statements.
- enum - It is used to define and declare user-defined data types.
- explicit - It is used for single argument constructors that can be used in typecasting.
- float - It is used to define floating-point numbers.
- for - It is used in (for loop statements) to achieve repetitive control over encapsulated statements.
- friend - Classes with the friend keyword can access the private data of members of a class.
- int - It is a fundamental data type used to define integer objects.
- long - It is used to define 32-bit int numbers.
- mutable - If we have a constant object and declare it mutable, its value can be changed at any time during compilation and runtime.
- new - It is used to allocate memory to an object in a program.
- operator - It is used to overload a c++ operator with a new declaration.
- private - It is used to declare class members which are not visible outside the class.
- protected - It is used to declare private class members except for the derived classes.
- public - It is used to declare class members that should be accessible outside the class.
- register - Frequently used objects are kept inside registers.
- return - It is used to return an object when we call a function to perform some tasks.
- short - It is used to define a 16-bit int number.
- sizeof - It returns the size of an object in bytes.
- struct - It is used to declare new types containing data and member functions.
- static - The originality of an object-defined static exists throughout program execution.
- switch - It is used specifically for switch statements.
- this - It is a pointer that points to an object or an instance of a class created.
- throw - It is used to generate exceptions during the compilation of programs.
- true - It is a boolean value, same as '1', resulting from an operation performed.
- try - It indicates the start of the exception handling block.
- typedef - It is used for user-defined data types and acts as an alias for existing data types.
- union - It can store only one type of data member at a given time.
- virtual - It declares a class member function as virtual that a derived class will redefine.
- void - It refers to the absence of a data type or function parameters list.
- while - It is used to start a while statement and end a do-while statement.
List of C++ Keywords not available in C Language
There are a total of 30 Keywords in C++ which are not available in C language, illustrated below:
asm | bool | class | catch | const_cast | delete |
dynamic_cast | explicit | friend | false | inline | mutable |
namespace | new | operator | protected | public | private |
reinterpret_cast | static_cast | template | throw | typename | true |
typeid | this | try | using | virtual | wchar_t |
Type Qualifiers in C++
Type Qualifiers in C++ are applied to already defined data types and don't change the meaning of variables. Instead, they provide users with some extra information about the properties of the variable. For example - If we use a const qualifier against an integer variable, it means the value is constant in the memory, and further, we can't change it.
These type qualifiers in C++ can be classified as:
Const
Const qualifier defines that the variable is constant. We can't change or modify the value; if we try to do so, it will give a compile-time error.
Syntax for const type qualifier:
Example:
It will give a compile-time error as we try changing a constant variable - 'name.'
Volatile
A volatile object or a variable can be changed at any time in the program. For example, a variable declared as volatile can be updated using a system clock or even by reference to another program.
Syntax for volatile type qualifier:
Example:
Here, the value is copied from memory to the CPU registers, and operations are performed.
The value is not copied from memory to the CPU registers as volatile is present.
Mutable
Mutable type-qualifier can make const-class members (constant) modifiable. In simple words, once a member is declared mutable, we can change its value anytime in the program code, even if the object created is const-type.
Syntax for mutable type qualifier:
Example:
Restrict
Restrict type qualifier is used as a pointer but doesn’t provide any functionality to the code. Compilers can optimize the code in presence of restrict keyword.
Example:
If we are using restrict with a pointer itr, it means itr is the only way to access the object pointed by it, and there is no other alternative pointer to access the same object. Hence, the compiler doesn't need any additional checks.
C++ Program to understand Type Qualifiers:
In this program, we will understand the use of const, mutable and volatile type-qualifiers. Here, non-const num is declared 0, so its value can be changed. num1 is const-type, and changing its value will produce a compile-time error. num2 is declared with a volatile qualifier; hence, its value can also be changed at any time. In a non-static class Test, variable yy is declared mutable; hence, even if we create a const object of this class, we can always modify the value of variable yy.
Output:
Identifiers in C++
Identifiers in C++ are short and informative names uniquely identifying C++ variables or function names. They are user-defined words. We can't use keywords as identifiers because keywords are pre-defined reserved words.
Difference between Keywords and Identifiers
- Keywords should be in lower cases always, but Identifiers can be both in upper and lower cases.
- Keywords can contain only alphabets, whereas Identifiers can contain alphabets, numeric digits, underscores, and special characters.
- Keywords are pre-defined reserved words, whereas identifiers are user-defined words used to identify variables uniquely.
- Some examples of valid Keywords: double, int, auto, char, break. Some examples of valid Identifiers: Test, Scaler_Academy, count1.
C++ Program to illustrate the use of Keywords:
Here is an example involving various keywords. We have used switch statements for multiple cases as input to print result values. Here as the input value is 2 in the program, hence "int keyword" will be printed.
Output:
C++ Program to illustrate the use of Identifiers:
In this program, we are initializing two valid identifier variable names, i.e., scaler_academy and count1, and thus, printing their result values.
Output:
Conclusion
- Tokens are the smallest elements of a program, and they can be classified into keywords, identifiers, constants, or strings.
- Keywords in C++ are the collection of reserved words. These are written in lower cases, and we have 95 keywords in C++ available.
- Identifiers in C++ are short and informative names uniquely identifying variables.