CS Engineering Gyan

Tokens in C++

In C++, a program is made up of small building blocks known as tokens. These are the smallest individual units that the compiler understands while processing a program.

Every statement in a C++ program is formed using different types of tokens. Understanding tokens is essential because they form the foundation of programming syntax.

Tokens in C++ Diagram

Types of Tokens in C++

C++ tokens are mainly divided into the following categories:

1. Keywords in C++

In C++, keywords are a set of reserved words that have predefined meanings assigned by the language. These words are an essential part of the syntax and are used by the compiler to understand the structure and behavior of a program.

Since keywords are already defined by the C++ language, they cannot be used as identifiers such as variable names, function names, or class names. Attempting to use a keyword for any other purpose will result in a compilation error.

๐Ÿ”น Why Keywords Are Important?

๐Ÿ”น Commonly Used Keywords

Here are some frequently used keywords in C++:

int       float     double    char
if        else      switch    case
for       while     do        break
continue  return    void      const
class     public    private   protected

๐Ÿ”น Categories of Keywords

C++ keywords can be grouped based on their functionality:

๐Ÿ”น Example Program Using Keywords

#include <iostream>
using namespace std;

int main() {
    int number = 10;

    if (number > 0) {
        cout << "Positive number";
    } else {
        cout << "Negative number";
    }

    return 0;
}

In the above program, words like int, if, else, return are keywords. Each one has a fixed role that helps define how the program behaves.

๐Ÿ”น Key Points to Remember

Overall, keywords act as the building blocks of C++ syntax, allowing programmers to write meaningful and structured programs efficiently.

2. Identifiers in C++

In C++, identifiers are the names assigned by the programmer to different elements of a program. These elements may include variables, functions, arrays, classes, objects, or any other user-defined items. Identifiers help make a program readable and meaningful by giving recognizable names to data and operations.

Instead of using memory addresses or complex representations, programmers use identifiers to refer to data easily. For example, a variable storing marks can be named marks, making the code more understandable.

๐Ÿ”น Importance of Identifiers

๐Ÿ”น Rules for Naming Identifiers

C++ follows strict rules for defining identifiers. These rules must be followed to avoid errors:

๐Ÿ”น Examples of Valid Identifiers

int marks;
float totalScore;
double average_value;
char grade;

๐Ÿ”น Examples of Invalid Identifiers

int 1marks;     // Starts with a number
float total-score;  // Contains special character (-)
int if;         // Keyword used as identifier
char student name;  // Contains space

๐Ÿ”น Naming Conventions (Best Practices)

Although not enforced by the compiler, following good naming practices is highly recommended:

๐Ÿ”น Example Program Using Identifiers

#include <iostream>
using namespace std;

int main() {
    int marks = 85;
    float totalScore = 100.0;
    float percentage = (marks / totalScore) * 100;

    cout << "Percentage: " << percentage;

    return 0;
}

In this example, marks, totalScore, and percentage are identifiers that store values and make the program easy to understand.

๐Ÿ”น Key Points to Remember

In summary, identifiers act as labels for different components in a C++ program, making it easier for programmers to write, read, and manage code effectively.

3. Constants in C++

In C++, constants are values that remain fixed throughout the execution of a program. Unlike variables, once a constant is defined, its value cannot be modified or reassigned. Constants are useful when you need to store values that should stay unchanged, such as mathematical values, fixed limits, or configuration data.

๐Ÿ”น Why Use Constants?

๐Ÿ”น Types of Constants

C++ supports different types of constants based on the kind of data they represent:

๐Ÿ”น Defining Constants in C++

Constants are commonly defined using the const keyword. Once declared, their value cannot be changed later in the program.

const int x = 10;
const float PI = 3.14;
const char grade = 'A';

๐Ÿ”น Example Program

#include <iostream>
using namespace std;

int main() {
    const float PI = 3.14;
    float radius = 5;

    float area = PI * radius * radius;

    cout << "Area of circle: " << area;

    return 0;
}

In this example, PI is a constant whose value remains unchanged throughout the program.

๐Ÿ”น Important Points to Remember

In summary, constants provide a safe way to store fixed values in a program, ensuring that important data remains unchanged and the code stays reliable.

4. Strings in C++

In C++, a string is a sequence of characters enclosed within double quotes (" "). Strings are mainly used to store and display text such as names, messages, and sentences in a program.

Each character in a string is stored in a continuous memory location, making it easy to handle and manipulate text data in programs.

๐Ÿ”น Basic Examples of Strings

"Hello World"
"Welcome to C++"
"Programming is fun"

๐Ÿ”น Using Strings in C++

Strings can be used directly in output statements or stored in variables using the string data type (from the standard library).

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World";
    return 0;
}

๐Ÿ”น Example with String Variable

#include <iostream>
#include <string>
using namespace std;

int main() {
    string message = "Welcome to C++";

    cout << message;

    return 0;
}

๐Ÿ”น Important Features of Strings

๐Ÿ”น Key Points to Remember

In short, strings make it easy to work with text in C++ programs, whether it's displaying messages or handling user input.

5. Operators in C++

In C++, operators are special symbols used to perform operations on variables and values. They allow programmers to manipulate data, perform calculations, and make logical decisions within a program.

Operators work with operands (values or variables) to produce a result. For example, in the expression a + b, + is the operator, while a and b are operands.

๐Ÿ”น Types of Operators

C++ provides different types of operators based on the kind of operations they perform:

๐Ÿ”น Example of Operators

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;

    int sum = a + b;        // Arithmetic operator
    bool result = (a > b);  // Relational operator

    cout << "Sum: " << sum << endl;
    cout << "Is a greater than b? " << result;

    return 0;
}

๐Ÿ”น Important Points

In summary, operators are the building blocks of expressions in C++, allowing you to perform everything from simple calculations to complex logical operations.

Example of Tokens in a Program

#include <iostream>
using namespace std;

int main()
{
    int a = 5;
    int b = 10;
    int sum = a + b;

    cout << "Sum = " << sum;

    return 0;
}

In this program:

Why Tokens are Important?

Conclusion

Tokens are the fundamental elements of a C++ program. Every line of code is built using different types of tokens such as keywords, identifiers, constants, strings, and operators. A clear understanding of tokens helps beginners write correct and efficient programs.

โ† Previous: C++ OOPS Concepts Next: Data Types in C++ โ†’
Home Visit Our YouTube Channel