←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Tokens, Data Types & Reference Variables in C++

Study material and explanations for Unit 1

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++:

The 6 Categories of C++ Tokens
1. Keywords
Reserved words with fixed meanings that you cannot change (int, class, return, for).
2. Identifiers
Names that you give to variables, functions, and classes (rollNo, totalMarks, Student).
3. Constants (Literals)
Fixed values that never change during program execution (10, 3.14, 'A').
4. Strings
A sequence of characters written inside double quotes ("Hello Indore", "CampusNotes").
5. Operators
Symbols that perform calculations or comparisons (+, -, *, ==, ::).
6. Punctuators / Delimiters
Punctuation marks used to structure code (;, {, }, (, ), [, ]).

2. Rules for Naming Identifiers (Variables)

When creating variable or function names, always follow these 5 basic rules:

  1. Allowed Characters: You can use letters (a-z, A-Z), digits (0-9), and the underscore (_).
  2. First Character: The name must start with a letter or an underscore. It cannot start with a number (e.g., 1student is wrong, but student1 or _student is valid).
  3. Case-Sensitive: C++ treats uppercase and lowercase letters differently. marks, Marks, and MARKS are three completely different variables!
  4. No Keywords: You cannot use reserved keywords as variable names (e.g., you cannot name a variable int, class, or while).
  5. 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: const has a specific data type (int, float), so the compiler can check for errors. #define is just a dumb text replacement with no type checking.
  • Scope Control: const variables can be kept local inside a specific function or class. #define affects 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:

  1. 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.
  2. 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 (total and Total are different).
    • Cannot use C++ keywords (like for, class).

Frequently Asked 10-Mark Questions:

  1. 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, and typedef.
    • Explain derived types: arrays, functions, pointers, and reference variables with code examples.