Keywords in C++
Keywords in C++ are the collection of reserved words. These are written in lowercase 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.
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:
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 lowercase 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.
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
Aspect | Keywords | Identifiers |
---|---|---|
Definition | Predefined/reserved words | Values used to define programming items |
Case Sensitivity | Always in lowercase | Can start with uppercase or lowercase |
Purpose | Defines the type of entity | Classifies the name of the entity |
Character Composition | Contains only alphabetical characters | Can consist of alphabetical characters, digits, and underscores |
Special Symbols | No special symbols or punctuations | No special symbols or punctuations. Only underscores are allowed |
Examples | int, char, while, do | Geeks_for_Geeks, GFG, Gfg1, myVariable, count |
Reserved for Syntax | Reserved for specific syntax and functionality | Created by programmers for their code |
Usage | Used to declare data types, control structures, etc. | Used for naming variables, functions, etc. |
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.