🔢 C++ Data Types – Understand Basic Types in C++
In C++, data types tell the compiler what kind of value a variable will hold. Think of them as labels on storage boxes 📦.
💡 Types of C++ Data Types:
int: Stores whole numbers (e.g.int age = 30;)float: Stores decimal numbers (single precision) (e.g.float temp = 36.6;)double: Stores decimal numbers (double precision) (e.g.double pi = 3.14159;)char: Stores a single character (e.g.char grade = 'A';)string: Stores text (e.g.string name = "Alice";)bool: Stores true or false (e.g.bool isOn = true;)
🎯 Example: Using Different Data Types
Let’s write a small program that uses all the basic data types:
#include <iostream>
using namespace std;
int main() {
int age = 25;
float height = 5.8;
double weight = 70.1234;
char grade = 'A';
string name = "Charlie";
bool isStudent = true;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Height: " << height << " ft" << endl;
cout << "Weight: " << weight << " kg" << endl;
cout << "Grade: " << grade << endl;
cout << "Student Status: " << isStudent << endl;
return 0;
}
🧠 Notes:
floatanddoublediffer in precision. Usedoublefor more accuracy.charvalues are enclosed in single quotes ('A'), whilestringuses double quotes ("Hello").boolreturns1for true and0for false when printed.