Understanding C++ Tokens, Data Types, Operators, and Type Casting

Markdown

View as Markdown

Every C++ program is built using fundamental elements known as tokens. Tokens are the smallest meaningful units recognized by the C++ compiler and include keywords, identifiers, variables, constants, operators, and literals. Understanding these building blocks is essential for writing correct and efficient C++ programs.

In addition to tokens, C++ provides type casting, which allows programmers to convert one data type into another when required. Type casting is commonly used when performing calculations involving different data types or when converting values for specific operations.

Learning Objectives

  • Understand C++ tokens

  • Identify keywords and identifiers

  • Declare variables and constants

  • Work with different data types

  • Use arithmetic and logical operators

  • Understand variable scope and lifetime

  • Perform implicit and explicit type casting

What are C++ Tokens?

Tokens are the smallest individual elements of a C++ program.

The compiler recognizes the following token types:

  • Keywords

  • Identifiers

  • Constants

  • Variables

  • Operators

  • String literals

  • Special symbols

Example:

int age = 25;

Here:

  • int → Keyword

  • age → Identifier

  • = → Operator

  • 25 → Constant

  • ; → Special symbol

Keywords

Keywords are reserved words that have predefined meanings.

Common C++ keywords include:

int

float

double

char

class

public

private

return

if

else

while

for

Keywords cannot be used as variable names.

 

Identifiers

Identifiers are names assigned to variables, classes, functions, and objects.

Example:

int studentAge;

float salary;

char grade;

Choose meaningful names that clearly describe their purpose.

Variables and Data Types

Variables store values during program execution.

Example:

#include <iostream>

using namespace std;

int main()

{

    int age = 20;

    float marks = 85.5;

    char grade = 'A';

    cout << "Age: " << age << endl;

    cout << "Marks: " << marks << endl;

    cout << "Grade: " << grade << endl;

    return 0;

}

Output

Age: 20

Marks: 85.5

Grade: A

Constants

Constants store values that remain unchanged.

Example:

const float PI = 3.14159;

Using constants improves code readability and maintainability.

Operators

C++ supports several categories of operators.

Arithmetic Operators

+

-

*

/

%

Example:

int total = 10 + 20;

Relational Operators

==

!=

> 

< 

>=

<=

Logical Operators

&&

||

!

Assignment Operators

=

+=

-=

*=

/=

Scope and Lifetime of Variables

Local Variable

A local variable is declared inside a function and can only be accessed within that function.

Example:

int main()

{

    int age = 20;

}

Global Variable

A global variable is declared outside all functions and can be accessed throughout the program.

Example:

#include <iostream>

using namespace std;

int age = 20;

int main()

{

    cout << age;

    return 0;

}

Type Casting

Type casting converts one data type into another.

There are two common types:

Implicit Type Casting

Performed automatically by the compiler.

Example:

int number = 10;

double value = number;

Explicit Type Casting

Performed manually by the programmer.

Example:

#include <iostream>

using namespace std;

int main()

{

    double marks = 89.75;

    int total = (int)marks;

    cout << total;

    return 0;

}

Output

89

Scope Example

#include <iostream>

using namespace std;

int number = 100;

int main()

{

    int local = 50;

    cout << "Global: " << number << endl;

    cout << "Local: " << local << endl;

    return 0;

}

Output

Global: 100

Local: 50

Best Practices

  • Use meaningful variable names.

  • Declare variables close to where they are used.

  • Use constants for fixed values.

  • Avoid unnecessary global variables.

  • Use explicit type casting only when required.

  • Keep expressions simple and readable.

Common Mistakes

  • Using keywords as variable names.

  • Forgetting to initialize variables.

  • Confusing assignment (=) with comparison (==).

  • Using unnecessary type casting.

  • Accessing local variables outside their scope.

Was this article helpful?

Still need help?

Contact us