In C++, data types tell the compiler what kind of data a variable will store. Different data types help the program use memory efficiently and understand how to handle the data.
| Data Type | Description | Example |
|---|---|---|
int |
Stores whole numbers (integers) like -10, 0, 100. | int age = 25; |
float |
Stores decimal numbers with single precision. | float price = 99.99; |
double |
Stores decimal numbers with double precision (more accurate than float). | double pi = 3.14159; |
char |
Stores a single character like 'A', 'z', or '9'. | char grade = 'A'; |
bool |
Stores only two values: true or false. |
bool isPassed = true; |
string |
Stores a sequence of characters (text). | string name = "Alice"; |
#include <iostream>
#include <string>
using namespace std;
int main() {
int age = 21;
float height = 5.7;
double weight = 68.5;
char grade = 'A';
bool isStudent = true;
string name = "Ravi";
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Height: " << height << " feet" << endl;
cout << "Weight: " << weight << " kg" << endl;
cout << "Grade: " << grade << endl;
cout << "Is Student? " << isStudent << endl;
return 0;
}
To use string, you need to include the <string> library. For input/output operations, include <iostream>.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!