Tokens, Data Types & Reference Variables in C++
1. What are Tokens in C++? (In Simple Words)
Just like words and punctuation marks form an English sentence, tokens are the smallest individual building blocks of a C++ program.
Whenever you compile your code, the compiler breaks your program down into these individual tokens.
There are 6 types of tokens in C++:
int, class, return, for).rollNo, totalMarks, Student).10, 3.14, 'A')."Hello Indore", "CampusNotes").+, -, *, ==, ::).;, {, }, (, ), [, ]).2. Rules for Naming Identifiers (Variables)
When creating variable or function names, always follow these 5 basic rules:
- Allowed Characters: You can use letters (
a-z,A-Z), digits (0-9), and the underscore (_). - First Character: The name must start with a letter or an underscore. It cannot start with a number (e.g.,
1studentis wrong, butstudent1or_studentis valid). - Case-Sensitive: C++ treats uppercase and lowercase letters differently.
marks,Marks, andMARKSare three completely different variables! - No Keywords: You cannot use reserved keywords as variable names (e.g., you cannot name a variable
int,class, orwhile). - No Spaces or Special Symbols: Spaces and symbols like
@,#,!,%are strictly not allowed.
3. Constants: Making Values Unchangeable
A Constant is a value that cannot be changed once defined.
In C++, you can create constants in two ways:
// Method 1: Using #define (Old C style)
#define PI 3.14159
// Method 2: Using the 'const' keyword (Modern C++ style - Recommended)
const float PI = 3.14159f;
const int MAX_STUDENTS = 60;
Why is const better than #define?
- Type Safety:
consthas a specific data type (int,float), so the compiler can check for errors.#defineis just a dumb text replacement with no type checking. - Scope Control:
constvariables can be kept local inside a specific function or class.#defineaffects the whole file.
4. Data Types in C++
Data types tell the compiler what kind of value a variable will hold and how much memory (RAM) it needs.
┌───────────────────────────┐
│ DATA TYPES IN C++ │
└─────────────┬─────────────┘
│
┌────────────────────────────────┼────────────────────────────────┐
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Basic (Built-in)│ │ User-Defined │ │ Derived │
├─────────────────┤ ├─────────────────┤ ├─────────────────┤
│ int (Integer) │ │ struct │ │ Arrays │
│ char (Character)│ │ union │ │ Functions │
│ float (Decimal) │ │ enum │ │ Pointers (*) │
│ double (Precise)│ │ class │ │ References (&) │
│ void (Empty) │ │ typedef │ │ │
│ bool (Boolean) │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
Basic (Built-in) Data Types:
| Data Type | Memory Size | What It Stores | Example |
|---|---|---|---|
int |
4 Bytes (32-bit) | Whole integers (positive or negative) | int age = 20; |
char |
1 Byte | Single character inside single quotes | char grade = 'A'; |
float |
4 Bytes | Decimal numbers (up to 7 digits precision) | float price = 99.50; |
double |
8 Bytes | High-precision decimal numbers (up to 15 digits) | double pi = 3.14159265; |
bool |
1 Byte | Boolean values: true (1) or false (0) |
bool isPassed = true; |
void |
0 Bytes | Represents empty / no value (used in functions) | void printMessage(); |
5. Reference Variables in C++ (The “Nickname” Concept)
A Reference Variable is one of the most useful features of C++.
Real-Life Analogy: Suppose a student’s official name is Rajesh, but his family and friends call him Raju. Whether someone calls him Rajesh or Raju, they are talking about the same single person! If Raju eats a samosa, Rajesh’s stomach gets full!
In C++, a reference variable is an alias (another name) for an already existing variable. Both share the exact same memory location.
Syntax:
data_type &reference_name = original_variable;
Simple Code Example:
#include <iostream>
using namespace std;
int main() {
int marks = 80;
int &score = marks; // score is a reference (alias) for marks
cout << "Original marks: " << marks << endl; // Prints 80
cout << "Reference score: " << score << endl; // Prints 80
// Changing value using the nickname
score = 95;
cout << "Updated marks: " << marks << endl; // Prints 95!
return 0;
}
6. Reference Variables vs. Pointers (Exam Comparison)
Students often get confused between a reference and a pointer. Here is a crystal clear comparison:
| Feature | Reference Variable (&ref) |
Pointer (*ptr) |
|---|---|---|
| Basic Meaning | An alias (another nickname) for an existing variable | A variable that stores the memory address of another variable |
| Syntax | int &ref = a; |
int *ptr = &a; |
| Initialization | Must be initialized immediately when created | Can be left uninitialized (though not recommended) |
| Can it be NULL? | No! A reference can never be NULL | Yes, can be NULL or nullptr |
| Can it point to another variable? | No! Once linked to a, it can never be changed |
Yes, can easily change to hold another variable’s address |
| Dereference Symbol | No symbol needed; use directly like a normal variable | Needs asterisk *ptr to read or modify the value |
| Memory Address | Shares the exact same address as the original variable | Has its own separate memory location on the stack |
DAVV Exam Preparation Tips
Frequently Asked 5-Mark Questions:
- What is a reference variable in C++? How does it differ from a pointer?
Model Answer:- Define reference as an alias sharing the same memory location (
int &ref = a;). - Draw a 3-column table comparing Reference vs. Pointer on: initialization, NULL capability, syntax, and address changes.
- Define reference as an alias sharing the same memory location (
- List any four rules for naming identifiers in C++.
Model Answer:- Must begin with an alphabet or underscore
_. - Cannot start with a number.
- Case-sensitive (
totalandTotalare different). - Cannot use C++ keywords (like
for,class).
- Must begin with an alphabet or underscore
Frequently Asked 10-Mark Questions:
- Explain the classification of data types in C++ with examples. Discuss basic, user-defined, and derived types.
Model Answer:- Draw the hierarchical data type chart (Basic, User-Defined, Derived).
- Detail basic data types (
int,char,float,double,bool) with sizes and ranges. - Explain user-defined types:
struct,union,enum,class, andtypedef. - Explain derived types: arrays, functions, pointers, and reference variables with code examples.