What is a token?
Topic asked in February 2022 (CBCS) and July 2022 (CBCS) question paper.
C++ Tokens are the smallest individual units of a program. Their are several types of tokens each of which serves a specific purpose in the syntax and semantics of C++.
Below are the main types of tokens in C++:
- Identifiers
- Keywords
- Constants
- Operators
Identifiers
In C++, an identifier is a name given to a variable, function, classes, etc. Identifiers are used to refer to these entities in the program, and they can consist of letters, digits, and underscores.
However, some rules must be followed when choosing an identifier:
- An identifier can consist of letters (A-Z or a-z), digits (0-9), and underscores (_). Special characters and spaces are not allowed.
- An identifier can only begin with a letter or an underscore only.
- C++ has reserved keywords that cannot be used as identifiers since they have predefined meanings in the language. For example, int cannot be used as an identifier as it has already some predefined meaning in C++. Attempting to use these as identifiers will result in a compilation error.
- As C++ is a case-sensitive language so identifiers such as 'first_name' and 'First_name' are different entities.
Here are some examples of valid and invalid identifiers in C++:
Valid:
- my_variable
- studentName
- balance_due
Invalid:
- my variable (contains a space)
- student# (contains a special character)
- int (same as a keyword)
C++ Identifiers Example
The following C++ program will provide you with an example of how Identifiers can be used in the C++ programming language:
Output:-
Explanation:-
In this program, the variable's age and name are identifiers used to store and access the values of the variables. The identifier age is used to store a person's age, and the identifier name is used to store the person's name. Notice how the identifiers are chosen to be descriptive and meaningful, which makes the code easier to read and understand. You can use any valid identifier in your C++ programs if you follow the rules for naming identifiers discussed earlier.
Keywords
Keywords are reserved words in C++ that have fixed meanings, and their meanings cannot be changed. The compiler already knows the meaning and functionality of these keywords.
Some of the keywords are:
bool | char | try | break |
default | long | short | do |
delete | new | sizeof | while |
else | private | this | throw |
double | int | void | return |
If you attempt to use a reserved keyword as an identifier, the compiler will produce an error. Here is an example of a program that tries to use a reserved keyword as an identifier:
C++ Keyword Example
Output:-
Explanation:-
When we compile the above code, the compiler produces an error because the int keyword cannot be used as an identifier. In summary, reserved keywords are an integral part of the C++ language and cannot be used as identifiers. You must choose a different name for your variables, functions, and other objects in your code.
Constants
In C++, a constant is a value that remains unchanged throughout the execution of a program. Constants are often used to represent fixed values, such as mathematical constants like π or predefined limits like the maximum size of an array.
To define a constant in C++, you use the const keyword followed by the data type and the constant's name, then initialize it with a value. Once defined, the value of a constant cannot be modified. Here is an example:
In this example, the constant PI is of type double and is initialized with the value of π. Constants enhance code readability and maintainability by providing descriptive names for fixed values.
Types of Constants in C++
-
Integer constants - These constants store values of the int data type. For example:
-
Floating constants - These constants store values of the float data type. For example:
-
Character constants - These constants store values of the chardata type. For example:
-
String constants - These constants are also of the character data type but declared differently. For example:
Operators
Operators in C++ are symbols used to perform operations on one or more operands. An operand is a value that an operator acts upon. C++ offers a wide range of operators for various operations.
Types Of Operators
- Arithmetic Operators - Used for mathematical operations like addition, subtraction, multiplication, etc.
- Relational Operators - Used for comparing values.
- Logical Operators - Used for combining conditions or negating conditions.
- Assignment Operators - Used for assigning values to variables.
- Bitwise Operators - Used for performing bit-level operations.
In addition to these, C++ also provides other operators like the sizeof operator, comma operator ( , ), address-of operator ( & ), and dot operator ( . ) for specific purposes.