Understanding Data Types in C++, With Examples

Monstar Studio / Shutterstock.com

Data types represent the fundamental blocks that construct virtually all programming languages. Without correct typing of data, manipulating the data becomes very unmanageable. Before you can choose a suitable type for your data, it s essential to understand what data types are and what options you have. In this article, we re going to cover the data types that are frequently used in C++, and how and when to use them.

What Are Data Types?

Data type refers to how the data is represented in code, and what characteristics it has. The data type restricts the properties of the data, what values it can have, and what operations can be used with it. Without data types, data would be unstructured and impossible to work with.

At a high level, we can categorize data into one of three types: basic (or primary), derived, or user-defined. Basic data types are built into the language and are the simplest to use. Derived data types build upon basic types, and hence tend to be more complex. User-defined types are created using basic types but can be implemented in specific ways depending on your needs. Next, we ll examine these in more detail.

Basic Data Types in C++

As mentioned, basic or primitive data types are those that are already built-in and are the most fundamental of all types. These include:

  • Integers These are numeric types of data, representing whole numbers, either negative or positive. They can be short , long or long long , usually meaning a size of 2, 4, or 8 bytes, respectively.
  • Floating-point These represent numbers with decimal places. Standard float represents one decimal place, while double represents two, and long double represents a more extended number.
  • Character We use characters to represent individual, non-numeric characters, i.e. %, $ and B, C, etc.
  • Boolean Logical values are indicated by a boolean, i.e. true or false.
  • Void -This is indicating the absence of a data type, usually used to return functions which don t have an associated value.

Example Implementation

To see how to implement these data types, consider the following code:

#include <iostream>

int main() {
    int myInt = 42;
    short myShort = 10;
    long myLong = 123456;
    long long myLongLong = 123456789;

    float myFloat = 3.14f;
    double myDouble = 2.71828;
    long double myLongDouble = 1.61803398875;

    char myChar = 'A';

    bool myBool = true;

    std::cout << "Integer: " << myInt << std::endl;
    std::cout << "Short: " << myShort << std::endl;
    std::cout << "Long: " << myLong << std::endl;
    std::cout << "Long Long: " << myLongLong << std::endl;

    std::cout << "Float: " << myFloat << std::endl;
    std::cout << "Double: " << myDouble << std::endl;
    std::cout << "Long Double: " << myLongDouble << std::endl;

    std::cout << "Character: " << myChar << std::endl;

    std::cout << "Boolean: " << std::boolalpha << myBool << std::endl;

    return 0;
}

This is a relatively simple example. We start by including the standard input/output stream header file, then we declare the main() function. Within this, we declare variables for each of the basic data types we ve discussed and then output the values to the console, as we can see in the image.

basic data types in C++.
Basic data types the most fundamental type in C++.

History-Computer.com

Derived Data Types in C++

As the name suggests, derived data types are based on primitive data types but are more complex. Within this category, we have:

  • Arrays These are fixed-size collections of elements that share a data type, stored in a contiguous memory block. We can access array elements using an index.
  • Pointers These variables store memory locations, directing to the location of a variable or object. Pointers are mostly used to allocate memory dynamically but also to implement complex algorithms and access certain data structures.
  • References These give an alternative name for a variable. References allow us to create several names for the same memory address and are often used to pass arguments by reference to a function. This lets us modify the initial variable.

Example Implementation

To demonstrate these data types, we have the following code:

#include <iostream>

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};

    std::cout << "Array elements: ";
    for (int i = 0; i < 5; ++i) {
        std::cout << numbers[i] << " ";
    }
    std::cout << std::endl;

    int* ptr = nullptr;
    ptr = numbers;

    std::cout << "Pointer elements: ";
    for (int i = 0; i < 5; ++i) {
        std::cout << *(ptr + i) << " ";
    }
    std::cout << std::endl;

    int x = 10;
    int& ref = x;

    std::cout << "Original value of x: " << x << std::endl;
    std::cout << "Value through reference: " << ref << std::endl;

    ref = 20;

    std::cout << "Modified value of x: " << x << std::endl;

    return 0;
}

Here, we ve declared an array, numbers , of size 5 and initialize it using the int [array name][number of elements] = {[elements] syntax. We then use afor loopto iterate over the elements in the array, and print them to the console.

Next, we declare the ptr pointer and assign it to the memory location of the array, and access the elements via the indices.

After that, we declare a variable, x , and create a reference, ref , that refers to it. The value of x is printed, and then we access the value through the reference. After modifying it, the modified value is printed as well.

derived data types in C++.
Derived data types are more complex than primitive types.

History-Computer.com

User-Defined Data Types in C++

The last kind of data type is known as user-defined. These are ones that programmers can create as and when they re required, and to suit their needs. While they re based on the basic data types, they often organize data in a more complicated manner. The user-defined types are:

Leave a Comment