Understanding Variables and Data Types in C++
In today’s post, we are going to learn one of the most fundamental concepts in C++ programming – Variables and Data Types. These are essential building blocks for writing any C++ program.
During my latest YouTube video, I explained these concepts in detail and also shared some example codes. Below are the notes from that video along with explanations and examples. You can download the complete notes file from the link at the end of this post.
✅ What is a Variable?
A variable in C++ is simply a name given to a memory location where we can store some data. Think of it like a box in your computer’s memory – the box has a name, and inside it, you can keep a value. You can change this value anytime and use it in calculations.
Example:
int age = 18;
Here, age is the variable name and it stores the value 18. The keyword int means this variable can only hold integer values.
✅ Types of Variables in C++
➤ Local Variable
A local variable is declared inside a function or block and can only be used within that block. Once the block ends, the variable is destroyed.
Example:
#include <iostream>
using namespace std;
int main() {
int x = 10; // local variable
cout << x;
return 0;
}
Here, x is a local variable inside the main() function. It cannot be accessed outside the main() function.
➤ Global Variable
A global variable is declared outside of all functions. It can be accessed by any function in the program.
Example:
#include <iostream>
using namespace std;
int g = 20; // global variable
int main() {
cout << g;
return 0;
}
Here, g is a global variable. It can be used anywhere in the program.
➤ Key Points about Variables
-
A variable must have a name and a data type.
-
A variable’s value can be changed during program execution.
-
Variables should always be declared before use.
-
The scope of a variable depends on whether it is global or local.
✅ What is a Data Type?
A data type tells the compiler what kind of data a variable can store. Without data types, the computer wouldn’t know if you are storing numbers, characters, or text.
✅ Types of Data Types in C++
There are mainly three categories of data types:
➤ Built-in Data Types
These are the basic data types provided by C++.
| Data Type | Description | Example |
|---|---|---|
| int | stores whole numbers | int age = 20; |
| float | stores decimal values | float pi = 3.14; |
| double | stores larger decimal values | double price = 99.99; |
| char | stores single characters | char grade = 'A'; |
| bool | stores true or false | bool isCodingFun = true; |
➤ User-defined Data Types
These are data types created by programmers.
struct Example:
struct Student {
int roll;
char grade;
};
class Example:
class Car {
public:
string brand;
int price;
};
typedef / using Example:
Used to create a new name for existing data types.
➤ Derived Data Types
These are data types built from basic ones.
Array Example:
int marks[5] = {90, 85, 80, 95, 88};
Pointer Example:
int a = 10;
int *ptr = &a;
Function Example:
Functions also act as derived data types because they return values of specific types.
➤ Key Points about Data Types
-
Data types define the size of memory allocated.
-
Wrong data type usage can cause errors or incorrect results.
-
Choosing the right data type makes programs more efficient.
✅ Download the Notes
For your reference and better understanding, I have prepared a detailed document containing all these notes along with the example codes explained in the video.
LINK TO CODE THAT I DID IN VIDEO
Feel free to comment below if you have any questions or want further clarifications. Keep practicing and happy coding!

0 Comments