Variables and Data Types in C++

              


📌 Variables in C++

🔹 What is a Variable?

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

👉 Example (simple):

int age = 20;
  • int → data type

  • age → variable name

  • 20 → value


🔹 Why Variables are Needed?

  • To store data

  • To change data during program execution

  • To perform calculations

👉 Example:

int a = 5; int b = 10; int sum = a + b;

🔹 Rules for Naming Variables

  1. Must start with a letter or underscore (_)

  2. Cannot start with a number ❌

  3. No spaces allowed

  4. Cannot use C++ keywords

✅ Valid:

int marks; int _total; int age1;

❌ Invalid:

int 1age; int total marks; int int;

🔹 Types of Variables (Based on Scope)

1️⃣ Local Variable

  • Declared inside a function

  • Used only inside that function

void show() { int x = 10; }

2️⃣ Global Variable

  • Declared outside all functions

  • Can be used anywhere

int x = 10;

📌 Data Types in C++

🔹 What is a Data Type?

A data type tells the compiler:

  • What type of data a variable will store

  • How much memory is needed


🔹 Main Types of Data Types in C++

1️⃣ Basic (Primitive) Data Types

Data TypeMeaningExample
intInteger numbersint a = 10;
floatDecimal numbersfloat b = 3.14;
doubleLarge decimalsdouble x = 10.5678;
charSingle characterchar ch = 'A';
boolTrue / Falsebool isPass = true;

🔹 int

Stores whole numbers

int marks = 95;

🔹 float

Stores decimal numbers (less precision)

float pi = 3.14;

🔹 double

Stores large decimal numbers (more precision)

double value = 123.456789;

🔹 char

Stores single character (in single quotes)

char grade = 'A';

🔹 bool

Stores only true or false

bool result = true;

2️⃣ Derived Data Types

🔹 Array

Stores multiple values of same type

int arr[3] = {10, 20, 30};

🔹 Pointer

Stores address of another variable

int x = 10; int *p = &x;

🔹 Function

Block of code that performs a task

void show() { cout << "Hello"; }

3️⃣ User-Defined Data Types

🔹 Structure (struct)

Used to store different data types together

struct Student { int roll; char grade; };

🔹 Union

Stores different data types but uses same memory

union Data { int a; float b; };

🔹 Enum

Used to define constant values

enum Day {Mon, Tue, Wed};

🔹 Size of Data Types (Important for Exams)

Data TypeSize
int4 bytes
float4 bytes
double8 bytes
char1 byte
bool1 byte

📌 Example Program (Very Simple)

#include <iostream> using namespace std; int main() { int age = 20; float height = 5.6; char grade = 'A'; bool pass = true; cout << age << endl; cout << height << endl; cout << grade << endl; cout << pass << endl; return 0; }

📌 Difference Between Variable and Data Type

VariableData Type
Stores dataDefines type of data
Has a nameHas a size
Example: ageExample: int

📌 Short Exam Answer (2–3 Lines)

Variables are containers used to store data in a program.
Data types specify the type of data a variable can store and the memory required.

          

📘 Want to learn C++ faster?

I recommend this beginner-friendly C++ course that explains

variables and data types with simple examples.


👉 Check this C++ course here  https://fkrtt.in/en/bB1ptn?user=




Comments

Popular posts from this blog

C++ Programming for Beginners – Step by Step Guide

If-Else Statement in C++