Difference Between Tokens Identifiers And Keywords In C++ - C++ Programming Tutorial
C++ Course / C++ vs Other Languages / Difference Between Tokens Identifiers And Keywords In C++

Difference Between Tokens Identifiers And Keywords In C++

BLUF: Mastering Difference Between Tokens Identifiers And Keywords In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Difference Between Tokens Identifiers And Keywords In C++

C++ is renowned for its efficiency. Learn how Difference Between Tokens Identifiers And Keywords In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the variances among Tokens, Identifiers, and Keywords in C++. Prior to delving into their distinctions, it is essential to comprehend Tokens, Identifiers, and Keywords in C++ along with their respective types and attributes.

What are Tokens?

Tokens represent the basic units of a C++ codebase, functioning as the foundational components of its syntax. These essential elements are crucial for the compiler to identify, shaping the organization and functionality of a C++ software project. They are categorized into distinct types, each playing a specific role in the execution of the program.

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:

  • Names such as variables, functions, classes, and user-defined entities are all considered as identifiers.
  • These labels are established by the programmer and act as pointers to specific memory locations, enabling manipulation and communication within the software.

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 values that remain unchanged throughout the execution of a program.
  • Examples include integer constants, floating-point constants, character constants, and string literals.
Example

const int MAX_COUNT = 100; // 'MAX_COUNT' is a constant token with a value of 100

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 specific values that are employed within the software.
  • 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 " ") serve as illustrations.
  • Tokens' Function in C++:

Tokens are the basic elements that constitute the syntax of C++ programs. These components are dissected, handled, and converted into instructions that can be understood by the computer through the compiler. Having a grasp of the different types of tokens and their purposes is essential for developing dependable and effective C++ code.

For instance, a code line such as int x = 10; consists of multiple tokens:

int: It serves as a reserved word that signifies the data type.

x: It serves as a symbol that denotes the name of the variable.

=: It is the assignment operator.

10: It represents an integer constant that holds the initial value assigned to variable x.

; The semicolon acts as a statement terminator.

What are Identifiers?

Identifiers serve as labels for different components within a C++ program in the coding realm. They are custom-defined symbols that are employed to denote variables, functions, classes, constants, labels, and user-defined types. These names play a crucial role in enhancing the readability, understanding, and manageability of the codebase.

Characteristics of Identifiers:

There are multiple attributes associated with identifiers. Some key features of identifiers include:

  • Identifiers may consist of letters (both upper and lower case), numbers, and underscores (_).

However, they must adhere to the following guidelines:

  • They must not begin with a digit.
  • Spaces and special characters, such as @, $, %, , are not permitted in identifiers.
  • Because C++ is case-sensitive, uppercase and lowercase letters are separate.
  • 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 various categories of identifiers in C++. Some primary classifications of the identifiers include:

Variable identifiers, also known as variable names, are labels given to locations in memory where information is stored. These names represent data that is capable of being altered during the execution of the program.

Example

int age; // 'age' is a variable identifier of type integer
double salary; // 'salary' is a variable identifier of type double

Function Identifiers:

  • Function identifiers are designated titles given to blocks of code that carry out specific tasks.
  • These titles are employed to call upon and run the respective block of code.
Example

void greetUser() { /* Code to greet the user */ } // 'greetUser' is a function identifier

Class Names:

  • Designations for classes are essential for establishing fresh data types through the creation of classes.
  • Within classes are both data and methods that manipulate this data.
Example

class Circle { /* Class definition for Circle */ }; // 'Circle' is a class identifier

Constant Identifiers:

  • Constant identifiers refer to names given to values that remain unchanged during the execution of a program.
  • These identifiers are created using the const keyword.
Example

const double PI = 3.14159; // 'PI' is a constant identifier with a fixed value

What are Keywords?

Keywords in C++ are predefined terms that hold specific meanings and roles within the language. These terms play a crucial role in explaining the structure, flow control, data types, and operations of a C++ program. Unlike regular identifiers, keywords are fixed and cannot be redefined since they serve fundamental purposes in the language, aiding in various programming tasks.

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++ includes a collection of predetermined keywords that are set aside for particular tasks. Some key categories of keywords include:

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 terms alter the functionality of variables, functions, or classes.

The keyword "const" signifies that the value of a variable cannot be altered.

A static variable's value persists across multiple function invocations.

public, private, and protected: These terms are employed to define access levels within classes (an integral part of Object-Oriented Programming principles).

Storage Class Keywords:

  • These terms control the lifetime and visibility of variable storage.
  • Examples of storage class keywords are auto, register, static, extern, and mutable.
  • Other Keywords:

C++ includes extra keywords that fulfill a range of purposes.

It serves as a keyword to exit a function and provide a return value.

The keyword "sizeof" is utilized to determine the size of a variable or data type.

Keywords for controlling and leveraging namespace elements.

Keywords for describing custom data types consist of class, struct, and enum.

Example:

Let's consider an instance to showcase Tokens, Keywords, and Identifiers in C++:

Example

#include <iostream>
int main() {
    // Keywords in C++
    int number = 10; // 'int' is a keyword, 'number' is an identifier
    if (number == 10) { // 'if' is a keyword
        std::cout << "The number is 10." << std::endl; // 'std::cout', '<<', 'endl' are tokens
    } else {
        std::cout << "The number is not 10." << std::endl;
    }
    // Identifiers in C++
    int a = 5; // 'a' is an identifier
    float b = 3.14; // 'b' is an identifier
    char c = 'A'; // 'c' is an identifier
    // Tokens in C++
    const double PI = 3.14159; // 'const', 'double', 'PI', '=', '3.14159', ';' are tokens
    std::string message = "Hello, World!"; // 'std', '::', 'string', 'message', '=', '"Hello, World!"', ';' are tokens
    return 0;
}
Output:
The number is 10.
Example 2:
Let us take another example to demonstrate Tokens, Keywords, and Identifiers in C++:
#include <iostream>
#include <string>
using namespace std;
int main() {
    // Keywords
    int num = 5;
    string name = "John";
    // Identifiers
    int total

Output:

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

There exist various distinctions among tokens, identifiers, and keywords in the C++ programming language. Here are some key variances:

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, names, and reserved words are essential components that influence the organization, flow, and operation of code within the C++ programming sphere. Distinguishing the distinctions among these fundamental elements is imperative for software engineers aiming to craft effective and accurate code. Proficiency in the interactions among reserved words, names, and tokens empowers programmers to construct robust, meticulously organized, and operational C++ software solutions.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience