×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Variables and Data Types in C++



Last Updated on: 4th Dec 2025 17:19:03 PM

Variables and data types form the foundation of any programming language.
In C++, which is a strongly typed and compiled language, understanding how data is stored, used, and processed is critically important.

 

This chapter provides a large, detailed explanation of variables, data types, memory size, rules, naming conventions, and practical real-life examples.

 

Every program needs to store data.
Example:

  • Age of a user

  • Price of a product

  • Student marks

  • Bank account balance

  • Student name

  • Temperature

  • Boolean flags (true/false)

 

C++ stores each type of data in variables, and each variable needs a data type that tells the compiler:

✔ What type of data will be stored
✔ How much memory to allocate
✔ What operations are allowed

 

C++ data types include:

  • Primitive (Built-in)

  • Derived

  • User-defined

 

This concept is extremely important because C++ is static-typed, meaning you must declare the type before using a variable.

 

What is a Variable?

A variable is a named memory location that stores data temporarily during program execution.

 

Definition (Simple)

A variable is a container used to store data in memory.

 

Definition (Technical)

A variable is an identifier associated with a memory address, allocated based on its data type, which determines the size and type of values it can hold.

 

Why Variables? (Real-Life Example)

Real Life In C++
A bank stores the balance of each customer C++ stores balance in a variable double balance;
A ticket counter stores number of tickets left int tickets;
A petrol pump stores fuel price float price;

 

Variables let a program remember information while it runs.

 

Declaring a Variable

 

Syntax:

data_type variable_name;

 

Examples:

int age;
float price;
char grade;

 

Declaring + assigning value:

int age = 20;
double rate = 99.5;

 

Rules for Naming Variables (Identifier Rules)

Variables in C++:

✔ Must begin with letter or underscore (_)
✔ Cannot start with a digit
✔ Can contain letters, digits, and underscore
✔ Cannot use keywords (int, class, etc.)
✔ Case-sensitive (Ageage)

 

Valid:

age, totalMarks, _salary, num1

 

Invalid:

1age, class, total marks

 

What are Data Types?

Data types define:

  • What type of data a variable can store

  • How much memory it will use

  • What operations can be performed

 

Types of Data Types in C++

C++ provides three categories:

1. Primitive (Built-in) data types
2. Derived data types
3. User-defined data types

 

1. Primitive / Built-in Data Types 

Primitive data types are basic types built into the language.

These directly map to hardware-level types.

Data Type Size Example Description
int 4 bytes 10, -20 integer values
float 4 bytes 10.5 single precision decimal
double 8 bytes 10.5423 double precision decimal
char 1 byte 'A', 'z' single character
bool 1 byte true/false logical values
void no size no return value
wchar_t 2–4 bytes wide character Unicode character

 

Example: Primitive Data Types

#include <iostream>
using namespace std;

int main() {
    int age = 21;
    float price = 299.99;
    double salary = 85000.50;
    char grade = 'A';
    bool isPass = true;

    cout << age << "\n" << price << "\n" << salary;
}

 

2. Derived Data Types 

These are derived from primitive types.

Examples:

  • Arrays

  • Pointers

  • Functions

  • References

Example:

int marks[5];

 

3. User-Defined Data Types : –

Created by programmers to represent complex data.

Examples:

  • struct

  • class

  • enum

  • union

  • typedef

Real-life example:

class Student {
public:
    int roll;
    string name;
};

 

Memory Size of Data Types (Table)

Type Size Range
char 1 byte -128 to 127
int 4 bytes -2B to 2B
float 4 bytes approx 7 digits
double 8 bytes approx 15 digits
bool 1 byte true/false

 

Type Modifiers in C++ 

Modifiers change the range of variables.

Modifiers:

signed
unsigned
short
long

 

Example:

unsigned int age = 20; // cannot store negative
long long population = 8000000000;

 

Remember: Using incorrect type can cause errors like overflow/underflow.

 

Real-Life Examples of Variables & Data Types

 

✔ Banking System

double balance;
int accountNumber;
string name;
bool isActive;

 

✔ Online Shopping

float productPrice;
int productId;
bool isDelivered;

 

✔ Gaming (Health, score, ammo)

int health = 100;
int score = 0;
float speed = 5.5;

 

Literal Values

A literal is a constant, fixed value written directly in the code.

 

Examples:

10     → integer literal
20.5   → float literal
'A'    → char literal
"Hello"→ string literal
true   → boolean literal

 

Constant Variables (const)

Constants cannot be changed after assignment.

 

Example:

const float PI = 3.14;

 

Type Casting 

Converting one data type into another.

 

Implicit (Automatic)

int a = 10;
float b = a;

 

Explicit (Manual)

float x = 9.7;
int y = (int)x;   // y becomes 9

 

Complete Example Program

#include <iostream>
using namespace std;

int main() {
    int age = 20;
    float price = 299.99;
    double salary = 90000.45;
    char grade = 'A';
    bool isPass = true;

    cout << "Age: " << age << endl;
    cout << "Price: " << price << endl;
    cout << "Salary: " << salary << endl;
    cout << "Grade: " << grade << endl;
    cout << "Pass Status: " << isPass;

    return 0;
}

 

Summary

  • A variable is a named memory location.

  • Data types decide what kind of data the variable stores.

  • C++ is strongly typed, so every variable must have a data type.

  • Primitive, derived, and user-defined types cover all kinds of data.

  • Type modifiers extend the range of data types.

  • Constants, literals, type casting expand the flexibility.

 

Keep practicing — you're doing amazing!

Happy Coding!    yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo