Data types specify what kind of value a variable can store. C++ provides different data types for integers, decimal numbers, characters, Boolean values, text, and more.
A data type tells C++ what kind of data a variable is intended to store.
int age = 20;
Here, int is the data type and age is the variable.
Data types help the compiler determine how values are represented and what operations are appropriate for them.
C++ data types can be broadly grouped into several categories.
Examples include int, double, arrays, pointers, classes, structures, and enumerations.
The int type is commonly used for whole-number values.
int age = 20;
int marks = 85;
int count = 100;
The exact range depends on the implementation, but an int is commonly at least 16 bits and often 32 bits.
The short type is an integer type that is typically smaller than or equal to an int.
short age = 20;
Its minimum guaranteed range is smaller than that of int.
The long type is an integer type that can provide at least as much range as int.
long population = 1000000L;
The size of long depends on the C++ implementation and platform.
The long long type provides an integer type with a large range.
long long distance = 9000000000LL;
It is useful when ordinary integer types are not large enough for the required values.
Signed integer types can represent both negative and positive values.
int temperature = -10;
int balance = 500;
For ordinary integer types, signed is the default unless otherwise specified.
Unsigned integer types represent non-negative values and can provide a larger positive range than the corresponding signed type.
unsigned int age = 25;
An unsigned integer cannot represent negative values.
The float type is used for floating-point values.
float price = 99.50f;
float temperature = 36.5f;
The f suffix can be used to make a floating-point literal a float.
The double type is commonly used when more floating-point precision is needed than a typical float.
double salary = 25000.75;
double percentage = 87.65;
The long double type is another floating-point type that can provide at least as much precision as double.
long double value = 12345.6789L;
Its size and precision are implementation-dependent.
The char type is used to store a single character.
char grade = 'A';
char symbol = '#';
Character literals are normally written using single quotation marks.
The bool type represents a logical value: true or false.
bool passed = true;
bool available = false;
C++ provides std::string in the standard library for storing text.
#include <string>
std::string name = "Rahul";
The <string> header should be included when using std::string.
The sizeof operator can be used to determine the size, in bytes, of an object or type.
#include <iostream>
int main() {
int age = 20;
std::cout << sizeof(age);
return 0;
}
The result depends on the implementation and platform.
#include <iostream>
int main() {
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(double) << std::endl;
std::cout << sizeof(char) << std::endl;
std::cout << sizeof(bool) << std::endl;
return 0;
}
This program displays the sizes of several types on the current implementation.
C++ provides type specifiers and modifiers such as:
These can be used with integer types to affect their range and representation.
unsigned int count = 100;
long long population = 1000000000LL;
short smallNumber = 10;
int number = 1000;
long largeNumber = 100000L;
long long veryLargeNumber = 9000000000LL;
Different integer types can be selected according to the range required by the program.
float price = 99.50f;
double salary = 25000.75;
long double measurement = 12345.6789L;
Floating-point types are useful for values that may contain a fractional part.
A declaration combines a type with a variable name.
int age = 20;
double price = 99.99;
char grade = 'A';
bool passed = true;
The data type tells the compiler what kind of value each variable represents.
A value can sometimes be converted from one type to another.
int number = 10;
double result = number;
Here, the integer value is converted to a double value when it is assigned to result.
Modern C++ provides the auto keyword to let the compiler deduce the type from the initializer.
auto age = 20;
auto price = 99.50;
The compiler deduces an appropriate type from each initializer.
Different types can have different sizes and alignment requirements. The exact size of many fundamental types is implementation-dependent.
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(double) << std::endl;
Use sizeof when your program needs to know the size on the current implementation.
Choose a data type based on the kind of value and range your program requires.
One common mistake is selecting a type that cannot represent the required value correctly.
int age = 25;
double price = 99.50;
For a value containing a fractional part, an appropriate floating-point type should normally be considered instead of an integer type.
#include <iostream>
#include <string>
int main() {
std::string name = "Amit";
int age = 20;
double marks = 87.5;
char grade = 'A';
bool passed = true;
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Marks: " << marks << std::endl;
std::cout << "Grade: " << grade << std::endl;
std::cout << "Passed: " << passed << std::endl;
return 0;
}
| Data Type | Example | Used For |
|---|---|---|
| int | int age = 20; |
Whole numbers |
| float | float price = 10.5f; |
Floating-point values |
| double | double salary = 25000.75; |
More precise floating-point values |
| char | char grade = 'A'; |
Single character |
| bool | bool passed = true; |
True/false values |
#include <iostream>
#include <string>
int main() {
int age = 21;
float height = 5.8f;
double salary = 25000.50;
char grade = 'A';
bool student = true;
std::string name = "Rahul";
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << std::endl;
std::cout << "Salary: " << salary << std::endl;
std::cout << "Grade: " << grade << std::endl;
std::cout << "Student: " << student << std::endl;
return 0;
}
This example demonstrates several commonly used C++ data types in one program.
C++ provides many data types so that programs can represent different kinds of information.
| Type | Example | Purpose |
|---|---|---|
| short | short n = 10; |
Short integer |
| int | int age = 20; |
Common integer values |
| long | long n = 100000L; |
Large integer values |
| long long | long long n = 9000000000LL; |
Very large integer values |
| float | float price = 10.5f; |
Floating-point values |
| double | double price = 10.5; |
Floating-point values with more precision |
| char | char grade = 'A'; |
Single character |
| bool | bool passed = true; |
Boolean values |
| std::string | std::string name = "Amit"; |
Text |
Question: Which data type is commonly used to store whole numbers in C++?