Similar to C, C++ also utilizes various data types to represent different kinds of data. Data types in C++ define the size, range, and operations allowed on the data stored in variables.
Table of Contents
Here’s a breakdown of the main data types in C++:
Built-in:
- Integer:
int
,short int
,long int
,long long int
, etc. – Stores whole numbers.
int x = 10;
- Floating-point:
float
,double
,long double
– Stores decimal numbers.
float y = 3.14;
- Character:
char
,wchar_t
– Stores single characters.
char ch = 'A';
- Boolean:
bool
– Stores true or false values.
bool isTrue = true;
Derived Data Types:
- Arrays: Fixed-size collections of elements of the same type.
int numbers[5] = {1, 2, 3, 4, 5}; // Array of 5 integers
- Pointers: Stores the memory address of another variable.
int age = 25;
int *agePtr = &age; // agePtr points to the memory address of variable 'age'
- References: Alias to an existing variable.
int number = 10;
int &numberRef = number; // numberRef and number refer to the same memory location
- Structures: Organize multiple variables of different types into a single unit.
struct Person {
std::string name;
int age;
};
- Unions: Stores one of several variables of different types within the same memory location.
union Data {
int number;
char character;
};
union Data data;
data.number = 10; // Stores the integer 10
data.character = 'a'; // Stores the character 'a' (only one value can be stored at a time)
- Classes: User-defined data types representing complex entities with attributes and methods.
class Dog {
public:
std::string name;
int age;
void bark() {
std::cout << "Woof!";
}
};
Other Data Types:
- void: Represents the absence of a value (used for functions that don’t return a value).
void printMessage() {
// This function doesn't return a value
std::cout << "This is a message";
}
- enum: Enumerated types, defining a set of named integer constants.
enum Color { Red, Green, Blue };
Color favoriteColor = Green;
- auto: Let the compiler deduce the data type automatically (C++11 and later).
Type Modifiers:
- Signed: Denotes that an integer can be positive, negative, or zero.
int age = 25; // signed int, can be positive, negative, or zero
- Unsigned: Denotes that an integer can only be positive or zero.
unsigned int score = 100; // unsigned int, can only be positive or zero
- Short: Reduces the size of an integer type.
short int smallNumber = 10; // smaller memory footprint than int
- Long: Increases the size of an integer type.
long int largeNumber = 1234567890; // larger range than int
Additional Types from C++11 and Later:
nullptr:
What it is:
- A keyword introduced in C++11 to represent a null pointer value.
- It’s a distinct type, different from any integral or pointer type.
- It can be assigned to any pointer type, but not to other types.
Usage examples:
- Assigning null to pointers:
int* ptr = nullptr; // ptr now points to nothing
- Checking for null pointers:
if (ptr == nullptr) {
// handle the null case
}
- Overloading functions based on null pointers:
void print(int* ptr) { ... }
void print(nullptr_t) { ... } // Overload for null pointers
char16_t:
What it is:
- A distinct character type introduced in C++11 to represent 16-bit characters.
- It’s primarily intended for working with Unicode characters, specifically UTF-16 encoding.
- It occupies 2 bytes of memory.
Key characteristics:
- Size: 16 bits (2 bytes).
- Encoding: UTF-16 (usually).
- Literals: Denoted by prefixing a string literal with
u
:u"Hello world"
. - Header: Requires
#include <cuchar>
header for functions and definitions.
Usage examples:
char16_t ch1 = u'A'; // Assign a single character
char16_t str[] = u"Hello"; // Create a string of char16_t characters
char32_t:
What it is:
- A distinct character type introduced in C++11 to represent 32-bit characters.
- It’s designed for working with large character sets, including Unicode characters.
- It occupies 4 bytes of memory.
Key characteristics:
- Size: 32 bits (4 bytes).
- Encoding: Often used for UTF-32 encoding, but not strictly tied to it.
- Literals: Denoted by prefixing a string literal with
U
:U"Hello world"
. - Header: Requires
#include <cuchar>
header for functions and definitions.
Usage examples:
char32_t ch = U'\u20AC'; // Assign a single Euro symbol character
char32_t str[] = U"Hello \u263A"; // Create a string with a smiley face
long long int:
What it is:
- An extended integer type introduced in C++98 to accommodate larger integer values than those supported by the standard
int
type. - It’s typically 8 bytes in size, but the exact size can vary across platforms.
Key characteristics:
- Size: Usually 8 bytes (64 bits).
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (approximately).
- Declaration: Uses the keyword
long long int
. - Literals: Suffixed with
ll
orLL
:42ll
or1234567890123456789LL
.
Usage examples:
long long int population = 8000000000; // Represents a large population
long long int age_of_universe = 13800000000000LL; // Represents a very large number
Size of data types:
The size of data types in C++ can vary depending on the system and compiler being used. However, some standard sizes are typically followed:
Data Type | Size (bytes) |
---|---|
char | 1 |
wchar_t | 2 or 4 |
short int | 2 |
int | 4 |
long int | 4 or 8 |
long long int | 8 |
float | 4 |
double | 8 |
long double | 8 or 16 |
bool | 1 |
void | 0 |
Key Points:
- Choose the appropriate data type based on the kind of data you need to store and the required precision.
- Use modifiers to optimize memory usage and value ranges.
- Derived data types offer more complex data structures.
- C++11 and later introduced additional data types and features.
See also: