Here’s a detailed list of important C++ data types with their subtypes, examples, and typical ranges.
C++ provides a
wide range of data types, each suited for specific use cases and varying memory constraints.
---
1. Integer Types:
- Description: Stores whole numbers, both positive and negative.
- Subtypes:
- int: Standard integer type.
- short int: Smaller integer type with a more limited range.
- long int: Allows larger integer values than int.
- long long int: Extended integer type with an even larger range.
- Examples:
```cpp
int a = 2147483647; // Standard int maximum value
short int b = 32767; // Max value for `short int`
long int c = 9223372036854775807; // Max value for `long int`
```
- Range:
- int: -2,147,483,648 to 2,147,483,647
- short int: -32,768 to 32,767
- long int: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- long long int: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
2. Floating-Point Types:
- Description: Used for numbers with fractional parts (decimals).
- Subtypes:
- float: Single-precision floating-point.
- double: Double-precision floating-point.
- long double: Extended precision floating-point.
- Examples:
```cpp
float x = 3.14f; // 7 decimal places of precision
double y = 2.718281828; // 15 decimal places of precision
```
- Range:
- float: ±1.2 × 10^-38 to ±3.4 × 10^38
- double: ±2.3 × 10^-308 to ±1.7 × 10^308
- long double: ±3.4 × 10^-4932 to ±1.1 × 10^4932
3. Boolean (bool):
- Description: Stores `true` or `false` values.
- Examples:
```cpp
bool isActive = true;
bool isComplete = false;
```
- Range: true or false.
4. Character (char):
- Description: Stores single characters, typically used for ASCII values.
- Subtypes:
- signed char: Can represent negative values.
- unsigned char: Represents only positive values.
- Examples:
```cpp
char letter = 'A'; // Standard char
unsigned char symbol = 65; // ASCII value for 'A'
```
- Range:
- char: -128 to 127 (for signed char), 0 to 255 (for unsigned char)
5. Wide Character (wchar_t):
- Description: Used to store larger character sets like Unicode.
- Examples:
```cpp
wchar_t wideChar1 = L'Ω'; // Unicode character for Omega
wchar_t wideChar2 = L'中'; // Chinese character
```
- Range: Typically 0 to 65,535 (varies by system).
6. String (std::string):
- Description: Stores a sequence of characters.
- Examples:
```cpp
std::string name = "Alice";
std::string greeting = "Hello, World!";
```
- Range: Limited by available memory.
7. Array:
- Description: Collection of elements of the same type.
- Examples:
```cpp
int arr[3] = {1, 2, 3}; // Array of integers
char chars[4] = {'A', 'B', 'C', '\0'}; // Array of characters
```
- Range: Size determined at initialization; typically limited by memory.
8. Pointers:
- Description: Stores memory addresses.
- Subtypes:
- int*: Pointer to an integer.
- char*: Pointer to a character.
- Examples:
```cpp
int num = 5;
int* ptr = # // Pointer to an integer
char* strPtr = "Hello"; // Pointer to a character array
```
- Range: Depends on system architecture (e.g., 32-bit or 64-bit addressable space).
9. Reference:
- Description: Alias for another variable.
- Examples:
```cpp
int num = 10;
int& ref = num; // Reference to `num`
```
- Range: Same as the referenced variable; has no distinct range on its own.
10. Enumeration (enum):
- Description: User-defined type with a set of named values.
- Examples:
```cpp
enum Days { MONDAY, TUESDAY, WEDNESDAY };
enum Colors { RED = 1, GREEN = 2, BLUE = 3 };
```
- Range: int range by default (0, 1, 2, ...), or can be assigned specific values.
11. Structure (struct):
- Description: User-defined type that can hold different data types.
- Examples:
```cpp
struct Person {
std::string name;
int age;
};
Person person = {"Alice", 30}; // Example of using a struct
```
- Range: Depends on the data types of the members.
12. Union:
- Description: Similar to `struct`, but shares memory among all members.
- Examples:
```cpp
union Data {
int intVal;
float floatVal;
};
Data data;
data.intVal = 10; // Storing an integer
data.floatVal = 5.5f; // Reuses the same memory for float
```
- Range: Depends on the data type of the largest member.
13. Void (void):
- Description: Represents an absence of type, often used for functions with no return value or generic pointers.
- Examples:
```cpp
void display(); // Function returning void
void* ptr; // Generic pointer
```
- Range: Not applicable, as it doesn’t represent any data.
---
This covers a wide range of C++ data types with their subtypes, examples, and ranges. These data types are
fundamental to C++ programming and allow for a variety of applications and flexibility in managing data. Let me
know if you need further details or additional types explained!