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++:

  1. Identifiers
  2. Keywords
  3. Constants
  4. 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:

identifiers.cpp
#include <iostream>
using namespace std;
 
int main()
{
    // Define a variable named "name" of type string and initialize it to "Eniv"
    string name = "Eniv";
    // Define a variable named "age" of type int and initialize it to 21
    int age = 21;
    // Print the value of the "name" and "age" variables
    cout << "My name is " << name << " and I am " << age << " years old." << endl;
 
    return 0;
}

Output:-

My name is Eniv and I am 21 years old.

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:

boolchartrybreak
defaultlongshortdo
deletenewsizeofwhile
elseprivatethisthrow
doubleintvoidreturn

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

keyword.cpp
#include <iostream>
using namespace std;
 
int main()
{
    // Define a variable named "int" of type int and initialize it to 10
    int int = 10;
    // Print the value of the "int" variable
    cout << "The value of the variable " << int << " is " << int << "." << endl;
 
    return 0;
}

Output:-

error: expected unqualified-id before numeric constant

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:

const double PI = 3.14159;

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:

    const int data = 7;
  • Floating constants - These constants store values of the float data type. For example:

    const float e = 2.71;
  • Character constants - These constants store values of the chardata type. For example:

    const char answer = 'e';
  • String constants - These constants are also of the character data type but declared differently. For example:

    const string name = "Eniv";

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

In addition to these, C++ also provides other operators like the sizeof operator, comma operator ( , ), address-of operator ( & ), and dot operator ( . ) for specific purposes.

How's article quality?

Page Contents