C++ Data Types
In C++, a data type specifies the type of data that a variable can store. Different data types require different amounts of memory and represent different kinds of values.
Basic Data Types
The following table shows some of the most commonly used data types in C++.
| Data Type | Description | Example |
|---|---|---|
| int | Used to store integer numbers | int age = 25; |
| float | Used to store decimal numbers | float price = 10.5; |
| double | Used to store large decimal numbers with more precision | double pi = 3.14159; |
| char | Used to store a single character | char grade = ‘A’; |
| bool | Used to store true or false values | bool isActive = true; |
| string | Used to store text | string name = “John”; |
Example Program
#include <iostream>
#include <string>
using namespace std;
int main() {
int age = 25;
float price = 19.99;
char grade = 'A';
bool isStudent = true;
string name = "Alice";
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Price: " << price << endl;
cout << "Grade: " << grade << endl;
cout << "Student: " << isStudent << endl;
return 0;
}
Output
Name: Alice Age: 25 Price: 19.99 Grade: A Student: 1
Important Notes
- Each variable must have a data type.
- Data types define the size and type of values that can be stored.
- Choosing the correct data type helps improve program efficiency.
Next Topic
Next, learn about C++ Constants.