Data Types, Variables and Tokens in C++: A Complete Beginner's Guide

Data Types, Variables and Tokens in C++: A Beginner's Guide
You know the basic structure of a C++ program. Now you need to know what goes inside that structure. Three concepts matter most: tokens, variables, and data types. Master these, and loops, functions, arrays, pointers, and object-oriented programming get easier.
Beginners often skip these basics and write code right away. Then they hit errors: invalid identifiers, wrong data types, missing semicolons. Let's cover each concept with real examples.
What Are Tokens in C++?
Tokens are the smallest meaningful pieces of a C++ program. An English sentence breaks into words and punctuation. A C++ program breaks into tokens.
Look at this line:
int age = 21;
This statement contains five tokens: int, age, =, 21, and ;. Each one does a job.
If you take an online C++ course, learn tokens first. You will see how the compiler reads your code, piece by piece.
Types of Tokens in C++
C++ tokens fall into five categories: keywords, identifiers, literals, operators, and punctuators. Strings are not a separate category. They fall under literals.
1. Keywords
Keywords are reserved words with a fixed meaning in C++. You cannot use them as names for your variables, functions, or classes.
intchar
float
if
else
return
class
2. Identifiers
Identifiers are names you create for variables, functions, classes, and objects.
agestudentName
totalMarks
calculateSalary
3. Literals
Literals are values written directly in your code. This includes integer, floating-point, character, Boolean, and string literals.
213.14
'A'
true
"Hello"
4. Operators
Operators are symbols that perform actions on values or expressions.
+-
*
/
=
==
++
<
>
5. Punctuators
Punctuators structure your code. Some people call them separators.
;,
()
{}
[]
| Token Category | Purpose | Examples |
|---|---|---|
| Keywords | Reserved words with fixed meanings | int, if, return |
| Identifiers | Names created by the programmer | age, marks, studentName |
| Literals | Values written directly in code | 10, 3.14, 'A', true |
| Operators | Perform operations | +, =, ==, ++ |
| Punctuators | Structure and separate code | ;, (), {}, [] |
Study these basics before a C++ certification course. They form the foundation for reading and writing C++ programs.
What Is a Variable in C++?
Picture a few boxes in your room. One holds "Age." Another holds "Books." A third holds "Money." Each box stores something different.
A variable works the same way. A variable is a named storage location for a value. You change that value while your program runs.
Definition: a variable is a named object used to store data in a C++ program.
int age = 21;float salary = 45000.50f;
char grade = 'A';
Here, age, salary, and grade are variables. Each holds a different data type.
Practice with examples like these. You will learn faster, whether you study alone or attend classes at a top C++ institute in Ghaziabad.
Rules for Naming Variables in C++
Follow these rules when you create a variable:
- Use letters, digits, and underscores only.
- Do not start a name with a digit.
- Do not use a C++ keyword as an identifier.
- Do not use spaces or characters like
@,#, and$. - Remember: C++ treats
age,Age, andAGEas three different identifiers.
int studentAge = 20; // Validint student_1 = 21; // Valid
int 1student = 22; // Invalid
int student age = 23; // Invalid
Apply these rules every time you practice, whether you study alone or search for C++ coaching near you.
What Are Data Types in C++?
A variable stores a value. A data type tells the compiler what kind of value the variable holds.
An int stores whole numbers. A char stores a single character.
Definition: a data type specifies the kind of value a variable holds. This shapes how the compiler stores and processes that value.
Learn data types before you move into an advanced C++ course.
1. Fundamental Data Types
| Data Type | Typical Size* | Example | Common Use |
|---|---|---|---|
int | 4 bytes | int age = 21; | Whole numbers |
float | 4 bytes | float price = 99.99f; | Decimal values |
double | 8 bytes | double pi = 3.14; | Decimal values that need more precision |
char | 1 byte | char grade = 'A'; | Single characters |
bool | Implementation-defined | bool isPassed = true; | True or false conditions |
void | No object storage | - | Represents no value, such as a function with no return value |
*Size depends on your compiler and platform. Check the size in your environment with sizeof().
2. Compound and Related Types
C++ also gives you types built from other types.
- Array: stores multiple elements of the same type.
int marks[5] = {90, 85, 88};
- Pointer: stores the address of an object or function.
int age = 21;int *ptr = &age;
- Reference: gives an existing object an alternative name.
- Function type: describes a function's parameter and return types.
These concepts grow more important as you move toward professional C++ programming, including the project work you find in a C++ course with placement support.
3. User-Defined Types
C++ lets you build your own types to represent complex data and program structures.
- Class: a core building block of object-oriented programming.
- Structure: a type you use to group related data.
- Union: lets different members share the same memory location.
- Enumeration: defines a set of named values.
class Student { // members
};
struct Book {
int id;
};
enum Day {
Monday,
Tuesday,
Wednesday
};
How to Check the Size of Data Types Using sizeof()
The sizeof() operator tells you the storage a type or object takes up in your environment.
#include using namespace std;
int main() {
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "Size of char: " << sizeof(char) << " byte" << endl;
return 0;
}
Output varies by compiler, architecture, and platform. Run sizeof() in your own setup to get an exact answer.
Variable vs Constant in C++
You change a variable's value after you initialize it. You cannot change a const object's value after initialization.
| Variable | Constant |
|---|---|
| Value can change | Value stays fixed after initialization |
int age = 21; | const int age = 21; |
| Use for values that change | Use for values that must stay fixed |
int age = 21;age = 22;
const int totalSubjects = 5;
// totalSubjects = 6; // Error
A Complete Practical Example
Before you compare C++ course fees, look at what practical learning looks like in code. This example combines variables, constants, data types, operators, and a conditional statement.
#include using namespace std;
int main() {
int studentAge = 20;
char studentGrade = 'A';
float percentage = 89.75f;
const int totalSubjects = 5;
bool isEligible = true;
cout << "--- Student Details ---" << endl;
cout << "Age: " << studentAge << endl;
cout << "Grade: " << studentGrade << endl;
cout << "Percentage: " << percentage << "%" << endl;
if (isEligible) {
cout << "Status: Eligible for scholarship" << endl;
}
return 0;
}
This program uses:
int,char,float, andboolas data types.studentAge,studentGrade, andpercentageas identifiers.20,'A', and89.75fas literals.=as an assignment operator.ifas a keyword.;,(), and{}to structure the program.constto locktotalSubjectsafter initialization.
Common Mistakes Beginners Make
1. Using an Uninitialized Variable
Reading the value of an uninitialized local variable causes undefined behavior. Initialize your variables.
int age = 0;
2. Confusing = and ==
Use = for assignment. Use == to check if two values are equal.
age = 21; // Assignmentage == 21; // Comparison
3. Using Invalid Variable Names
int student-age = 20; // Invalidint studentAge = 20; // Valid
4. Forgetting the Semicolon
int age = 20; // Correct
Most C++ statements need a semicolon at the end.
Wrap-Up
Tokens form the elements of C++ source code. Variables give you named storage for values. Data types define the kind of data those variables hold.
Learn these concepts first. Then operators, conditions, loops, functions, arrays, pointers, and object-oriented programming get easier.
When you choose a C++ course, look past the fees. Check the syllabus, practical training, project work, trainer experience, learning format, and career support.
Practical C++ Training with SoftCrayons
Ready to move past basic syntax and gain real programming experience?
SoftCrayons offers practical C++ training with programming assignments, hands-on projects, and guidance from experienced trainers. This approach helps you build a strong foundation and grow toward advanced C++ concepts.
Want details on the curriculum, upcoming batches, training format, or fees?
Connect with SoftCrayons today. Get the latest course details and start building your C++ skills through practical learning.



