Javatpoint Logo
Javatpoint Logo

Difference between Tokens, Identifiers, and Keywords in C++

In this article, we will discuss the difference between the Tokens, Identifiers, and Keywords in C++. But before discussing their differences, we must know about the Tokens, Identifiers, and Keywords in C++ with their types and characteristics.

What are Tokens?

Tokens are the smallest individual pieces of a C++ program, serving as the syntax's building blocks. They are fundamental parts that the compiler recognizes and that define the structure and logic of a C++ codebase. Tokens are divided into several sorts, each with its role in program execution.

Types of Tokens:

Tokens in C++ are classified into numerous types:

Keywords:

  • Keywords are predefined reserved terms in the C++ language that have unique meanings and functionalities.
  • These terms have distinct meanings and cannot be altered or used as identifiers.
  • Among the keywords in C++ are if, else, for, while, int, class, and return. They serve as the language's foundation, defining its core control structures and data types.

Identifiers:

  • Variables, functions, classes, and user-defined entities all have identifiers.
  • The programmer creates these names, which serve as references to memory regions, allowing manipulation and interaction within the program.

Identifiers adhere to certain guidelines:

  • They can be characters (both uppercase and lowercase), numbers, or underscores.
  • Identifiers must begin with a letter or a period.
  • They should not be confused with keywords.
  • In C++, identifiers are case-sensitive.

Constants:

  • In C++, constants are fixed values that do not change during program execution.
  • Integer constants, floating-point constants, character constants, and string literals are some examples.

Operators:

  • Operators are symbols in C++ that perform specified operations on operands.
  • Arithmetic operators (+, -, *, /), relational operators (, >, ==,!=), logical operators (&&, ||,!), assignment operators (=, +=, -=), and others are examples.
  • Operators assist in the execution of various computations and comparisons within a program.

Seperators and Delimiters:

  • Separators and delimiters are characters that are used to structure and separate various elements of the program.
  • Braces, parentheses (), commas, semicolons; and other symbols are examples.
  • They aid in the organization of code blocks and the definition of program grammar.

Literals:

  • Literals are explicit values that are utilized in the program.
  • Numeric literals (integer or floating-point numbers), character literals (single characters enclosed in single quotes''), and string literals (sequences of characters encased in double quotations " ") are examples.

Tokens' Function in C++:

Tokens are the fundamental parts that make up the syntax of C++ programs. They are the units analysed, processed, and translated into machine-readable instructions by the compiler. Understanding the various token types and their functions is critical for building reliable and efficient C++ code.

For example, a line of code like int x = 10; has many tokens:

int: It is a keyword that indicates the data type.

x: It is an identifier that represents the variable name.

=: It is the assignment operator.

10: It is an integer literal containing the initial value of variable x.

; The semicolon acts as a statement terminator.

What are Identifiers?

Identifiers are names for various parts of a C++ program in the programming world. They are user-defined tokens that can be used to represent variables, functions, classes, constants, labels, and user-defined types. Identifiers are extremely important in making code legible, comprehensible, and maintainable.

Characteristics of Identifiers:

There are several characteristics of the identifiers. Some main characteristics of the identifiers are as follows:

  • Identifiers can include letters (both uppercase and lowercase), numerals, and underscores (_).

However, they must adhere to the following guidelines:

  1. They must not begin with a digit.
  2. Spaces and special characters, such as @, $, %,, are not permitted in identifiers.
  3. Because C++ is case-sensitive, uppercase and lowercase letters are separate.
  4. For example, myVariable, MyVariable, and MYVARIABLE are all distinct identifiers.
  • Reserved Keywords: Identifiers and C++ keywords or reserved words that cannot be the same. Keywords are established keywords within a language that are used for certain purposes. For example, identifiers cannot be named int, class, if, while, and so on since these words have specified meanings in C++.
  • Meaningful and Descriptive: Choosing meaningful and descriptive identifiers is a smart practice. This activity aids in understanding the purpose of variables, functions, and other code components. For example, having totalAmount as an identifier for a variable that represents the total amount is more informative than having a general term like temp.

Types of Identifiers:

There are several types of identifiers in C++. Some main types of the identifiers are as follows:

Variable identifiers:

  • Variable IDs are names assigned to memory places where data is stored.
  • They indicate values that can change while the program is running.

Function Identifiers:

  • Function identifiers are names assigned to code blocks that execute certain activities.
  • These names are used to invoke and execute the corresponding code block.

Class Identifiers:

  • Class identifiers are names that are used to define new data types by creating classes.
  • Classes include data and functions that operate on that data.

Constant Identifiers:

  • Constant identifiers are names assigned to values that stay constant throughout programme execution.
  • They are made with the const keyword.

What are Keywords?

Keywords are reserved terms in C++ that have specialized definitions and functions inside the language. These phrases are essential in describing a C++ program's structure, control flow, data types, and functionality. Keywords cannot be redefined or used as identifiers because they are basic components of the language, and they have diverse functions in helping specific programming activities.

Keywords' Role in C++:

  • Keywords are essential in creating the syntax and logic of C++ programs.
  • They are an important aspect of the grammar of the language and are restricted for special uses.
  • C++ programmers utilize keywords to create data types, control flow structures, access modifiers, and other features. Keywords aid in outlining the program's behavior and functioning, assuring proper execution.

Keyword Categories:

C++ features a set of predefined keywords that are reserved for specific activities. Some of the most important keyword categories are:

Data Type Keywords:

  • In C++, these keywords are used to specify different data types.
  • int, char, float, double, bool: These are variable declaration keywords that represent integers, characters, floating-point numbers, and Boolean values.
  • void: It indicates that a function returns no value.

Control Flow Keywords:

  • Control flow keywords are used to direct the execution flow of a program.
  • if, otherwise: These keywords are associated with conditional branching.
  • because, while: These keywords are used to regulate loops.
  • case, break, and default: Switch-case statements utilize keywords to control program flow.

Modifiers and Specifiers Keywords:

These keywords change the behavior of variables, functions, or classes.

Const: It indicates that the value of an entity cannot be changed.

Static: A variable's value is retained between function calls.

public, private, and secure: These keywords are used to specify access levels in classes (part of the notions of Object-Oriented Programming).

Storage Class Keywords:

  • These keywords govern the duration and scope of variable storage.
  • Storage class keywords include auto, register, static, extern, and mutable.

Other Keywords:

C++ contains additional keywords that serve a variety of functions.

return: It is a keyword for exiting a function and returning a value.

sizeof: It is a keyword that specifies the size of a data type or variable.

namespace, employing: Keywords for managing and utilizing namespace members.

Keywords for defining user-defined data types include class, struct, and enum.

Example:

Let us take an example to demonstrate Tokens, Keywords, and Identifiers in C++:

Output:

Difference between Tokens, Identifiers, and Keywords in C++

Differences between Tokens, Identifiers, and Keywords in C++:

There are several difference between tokens, identifiers, and keywords in C++. Some main differences are as follows:

Aspect Tokens Identifiers Keywords
Definition It is the smallest individual unit in C++ syntax. User-defined names for program elements These are predefined reserved words in C++.
Roles These are used to define the structure and logic of a C++ codebase. It represents variables, functions, and classes. These are used to define specific functionalities in code.
Examples if, int, =, ; , +, 123, etc. variableName, functionName, className if, else, for, int, return, etc.
Types Keywords, operators, literals, etc. User-created names. Reserved words with specific meanings.
Categories Keywords are a subset of tokens. Identifiers are a type of token. Keywords are a distinct token category.
Purpose Define language syntax and semantics. Represent entities within the program Reserved for specific language purposes
Usage Construct statements and expressions Refer to memory locations. Define control flow, data types, etc.
Cannot be used It cannot be redefined or modified. It can be created by the programmer. It cannot be used as user-defined names.

Conclusion:

Tokens, identifiers, and keywords play critical roles in determining the structure, logic, and functioning of programs in the C++ programming domain. Recognizing the differences between these key pieces is required for programmers to develop efficient and correct code. Understanding how keywords, identifiers, and tokens interact allows developers to build strong, well-structured, and functional C++ applications.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA