Data Type

This article comprehensively sorts out the four major classifications of C++ data types (basic data types, constructed data types, pointer types, and void types).

I. Classification of Data Types

In the C++ language, data types can be divided into four categories: basic data types, constructed data types, pointer types, and void types.

II. Basic Data Types

The main feature of basic data types is that they cannot be decomposed into other data types. They are divided into the following categories:

1. Integer Types

NameSymbolNumber of BytesMinimum ValueMaximum ValueMaximum Digits (Decimal)
Short Integershort2 (16 bits)-32768327675
Integerint4 (32 bits)-2147483648214748364710
Long Long Integerlong long8 (64 bits)-9223372036854775808922337203685477580719
Unsigned Integerunsigned int4 (32 bits)04294967295

Relevant Test Code:

#include <iostream>
#include <limits>  
using namespace std; 

int main() {
    cout << "=== Verification of Data Type Sizes ===" << endl;
    // Byte size
    cout << "short bytes: " << sizeof(short) << " bytes" << endl;
    cout << "int bytes: " << sizeof(int) << " bytes" << endl;
    cout << "long long bytes: " << sizeof(long long) << " bytes" << endl;
    cout << "unsigned int bytes: " << sizeof(unsigned int) << " bytes" << endl;

    cout << "\n=== Value Ranges ===" << endl;
    // Value ranges
    cout << "short range: " << numeric_limits<short>::min() << " to " << numeric_limits<short>::max() << endl;
    cout << "int range: " << numeric_limits<int>::min() << " to " << numeric_limits<int>::max() << endl;
    cout << "long long range: " << numeric_limits<long long>::min() << " to " << numeric_limits<long long>::max() << endl;
    cout << "unsigned int range: " << numeric_limits<unsigned int>::min() << " to " << numeric_limits<unsigned int>::max() << endl;

    cout << "\n=== Bit Width ===" << endl;
    // Bit width (number of bytes × 8)
    cout << "short bit width: " << sizeof(short) * 8 << " bits" << endl;
    cout << "int bit width: " << sizeof(int) * 8 << " bits" << endl;
    cout << "long long bit width: " << sizeof(long long) * 8 << " bits" << endl;
    cout << "unsigned int bit width: " << sizeof(unsigned int) * 8 << " bits" << endl;

    return 0;
}

2. Character Type

NameSymbolNumber of Bytes
Characterchar1 (8 bits)

Relevant Code:

#include <iostream>
#include <limits>  
using namespace std; 

int main() {
    cout << "=== Verification of Data Type Sizes ===" << endl;
    // Byte size
    cout << "char bytes: " << sizeof(char) << " bytes" << endl;
   
    cout << "\n=== Bit Width ===" << endl;
    // Bit width (number of bytes × 8)
    cout << "char bit width: " << sizeof(char) * 8 << " bits" << endl;

    return 0;
}

3. Floating-Point Types

NameSymbolNumber of BytesMinimum ValueMaximum Value
Single Precisionfloat4 (32 bits)1.17549e-0383.40282e+038
Double Precisiondouble8 (64 bits)2.22507e-3081.79769e+308

Reference Code:

#include <iostream>
#include <limits>  
using namespace std; 

int main() {
    cout << "=== Verification of Data Type Sizes ===" << endl;
    // Byte size
    cout << "float bytes: " << sizeof(float) << " bytes" << endl;
    cout << "double bytes: " << sizeof(double) << " bytes" << endl;

    cout << "\n=== Value Ranges ===" << endl;
    // Value ranges
    cout << "float range: " << numeric_limits<float>::min() << " to " << numeric_limits<short>::max() << endl;
    cout << "double range: " << numeric_limits<double>::min() << " to " << numeric_limits<int>::max() << endl;
   
    cout << "\n=== Bit Width ===" << endl;
    // Bit width (number of bytes × 8)
    cout << "float bit width: " << sizeof(float) * 8 << " bits" << endl;
    cout << "double bit width: " << sizeof(double) * 8 << " bits" << endl;

    return 0;
}

III. Constructed Data Types

Constructed data types are defined by constructing one or more existing data types. In other words, the value of a constructed type can be decomposed into several "members" or "elements", where each member is either a basic data type or another constructed type.

2025121002_jeb3b.png

(1) Array Type

For example, the definition statement int a[10]; indicates that a is an integer array with a length of 10, containing 10 array elements from a[0] to a[9]. A contiguous block of memory is allocated for 10 int type data. If an int type occupies 4 bytes, a contiguous 40-byte space is allocated to store the 10 integer array elements. Arrays can be divided into one-dimensional, two-dimensional, and multi-dimensional arrays.

(2) Structure Type

Similar to arrays, structures group multiple data into a single unit, but their members can be of different data types. In the following example, a new structure type struct person is defined, whose member variables (the name array, agesex, etc.) belong to different data types:

struct person {
   char name[10];
   int age;
   char sex;
};

(3) Union Type

union Data {
    int i;
    float f;
    char str[20];
};

IV. Pointer Types

Pointer types are a special and important category of data types. Their uniqueness lies in that the value of a pointer represents the memory address of a variable. Although the value of a pointer variable is similar to that of an integer variable, they are entirely different types.

V. Void Type

The void type (empty type) is often used to indicate the return type of a function.

Subscribe to CodeEachDay

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe