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→ data type -
age→ variable name -
20→ value
🔹 Why Variables are Needed?
-
To store data
-
To change data during program execution
-
To perform calculations
👉 Example:
🔹 Rules for Naming Variables
-
Must start with a letter or underscore (_)
-
Cannot start with a number ❌
-
No spaces allowed
-
Cannot use C++ keywords
✅ Valid:
❌ Invalid:
🔹 Types of Variables (Based on Scope)
1️⃣ Local Variable
-
Declared inside a function
-
Used only inside that function
2️⃣ Global Variable
-
Declared outside all functions
-
Can be used anywhere
📌 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 Type | Meaning | Example |
|---|---|---|
int | Integer numbers | int a = 10; |
float | Decimal numbers | float b = 3.14; |
double | Large decimals | double x = 10.5678; |
char | Single character | char ch = 'A'; |
bool | True / False | bool isPass = true; |
🔹 int
Stores whole numbers
🔹 float
Stores decimal numbers (less precision)
🔹 double
Stores large decimal numbers (more precision)
🔹 char
Stores single character (in single quotes)
🔹 bool
Stores only true or false
2️⃣ Derived Data Types
🔹 Array
Stores multiple values of same type
🔹 Pointer
Stores address of another variable
🔹 Function
Block of code that performs a task
3️⃣ User-Defined Data Types
🔹 Structure (struct)
Used to store different data types together
🔹 Union
Stores different data types but uses same memory
🔹 Enum
Used to define constant values
🔹 Size of Data Types (Important for Exams)
| Data Type | Size |
|---|---|
| int | 4 bytes |
| float | 4 bytes |
| double | 8 bytes |
| char | 1 byte |
| bool | 1 byte |
📌 Example Program (Very Simple)
📌 Difference Between Variable and Data Type
| Variable | Data Type |
|---|---|
| Stores data | Defines type of data |
| Has a name | Has a size |
| Example: age | Example: 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
Post a Comment