📅 Date: Jan 2, 2026
🔥 Topic: Variables, Primitives & Data Types
📦 What are Variables?
Yesterday, I printed static text. But software needs to handle changing data. That's where Variables come in.
Think of a variable as a container or a box in the computer's memory where we store data. Every box needs two things:
- A Name: To identify it (e.g.,
age,score). - A Type: To tell the computer what kind of data is inside (Number? Text? Decimal?).
🗂️ Data Types in C++
C++ is a "strongly typed" language. This means if I create a box for an integer, I cannot force text into it. Here are the fundamental types I learned today:
- int: Stores whole numbers (e.g.,
25,-10). - float / double: Stores decimal numbers (e.g.,
9.8,3.14). Double is just a more precise float. - char: Stores a single character in single quotes (e.g.,
'A','$'). - string: Stores text in double quotes (e.g.,
"Sahil"). (Requires #include <string>) - bool: Stores logical values:
true(1) orfalse(0).
💻 Day 2 Code: Creating a User Profile
To practice this, I wrote a program that stores different types of data for a user profile and prints them out.
#include <iostream>
using namespace std;
int main() {
// 1. Integer (Whole Number)
int age = 19;
// 2. Float (Decimal)
float cgpa = 8.5;
// 3. Character (Single Letter)
char grade = 'A';
// 4. Boolean (True/False)
bool isCoder = true;
// 5. String (Text)
string name = "Sahil";
// Displaying the Data
cout << "--- ID CARD ---" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "CGPA: " << cgpa << endl;
cout << "Grade: " << grade << endl;
// Bool prints 1 for true, 0 for false
cout << "Is Coder? " << isCoder << endl;
return 0;
}
🧠Key Takeaway: Memory Size
I also learned that different types take up different amounts of space in memory (RAM).
intusually takes 4 bytes.chartakes only 1 byte.doubletakes 8 bytes.
We can check this using the sizeof() function!
💠Thoughts
Variables make code dynamic. Instead of hardcoding "Sahil", I can now store it in a variable `name` and change it whenever I want. Tomorrow, I plan to look into If/Else Conditionals to make decisions in code.