</>
Skip to content
C++ lessons (8/42)

C++ — Data Types

Fundamental types

CategoryTypes
Integerbool, char, short, int, long, long long
Floatingfloat, double, long double
Voidvoid

Integer types

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

int main() {
    bool flag = true;              // 1 byte
    char c = 'A';                 // 1 byte
    unsigned char uc = 255;       // 1 byte
    short s = -32000;             // 2 bytes
    int i = 2147483647;           // 4 bytes
    long l = 1000000L;            // 4 or 8 bytes
    long long ll = 9223372036854775807LL; // 8 bytes

    // Fixed-width types (C++11)
    int32_t fixed = -1000;
    uint64_t big = 18446744073709551615ULL;

    cout << "int32_t: " << fixed << endl;
    cout << "uint64_t: " << big << endl;

    return 0;
}

Floating-point types

#include <iostream>
using namespace std;

int main() {
    float f = 3.14f;              // 4 bytes, ~7 digits
    double d = 3.141592653589793; // 8 bytes, ~15 digits
    long double ld = 3.14L;       // 12-16 bytes

    cout << "float: " << f << endl;
    cout << "double: " << d << endl;
    cout << "long double: " << ld << endl;

    // Special values
    cout << "Infinity: " << 1.0 / 0.0 << endl;
    cout << "NaN: " << 0.0 / 0.0 << endl;

    return 0;
}

The string class

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

int main() {
    string s1 = "Hello";
    string s2 = "World";
    string s3 = s1 + " " + s2;

    cout << s3 << endl;            // Hello World
    cout << s3.length() << endl;   // 11
    cout << s3.substr(0, 5) << endl; // Hello

    // String interpolation with to_string
    int age = 30;
    string greeting = "Age: " + to_string(age);
    cout << greeting << endl;

    return 0;
}

Arrays

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

int main() {
    // C-style array
    int arr[] = {1, 2, 3, 4, 5};
    cout << "C-array size: " << sizeof(arr) / sizeof(arr[0]) << endl;

    // std::array (C++11, preferred)
    array<int, 5> stdArr = {10, 20, 30, 40, 50};
    cout << "std::array size: " << stdArr.size() << endl;

    for (const auto &val : stdArr) {
        cout << val << " ";
    }
    cout << endl;

    return 0;
}

Structs and classes

#include <iostream>
using namespace std;

struct Point {
    double x, y;

    double distanceTo(const Point &other) const {
        double dx = x - other.x;
        double dy = y - other.y;
        return sqrt(dx * dx + dy * dy);
    }
};

class Circle {
private:
    Point center;
    double radius;
public:
    Circle(Point c, double r) : center(c), radius(r) {}
    double area() const { return 3.14159 * radius * radius; }
};

int main() {
    Point p1 = {0, 0};
    Point p2 = {3, 4};
    cout << "Distance: " << p1.distanceTo(p2) << endl;

    Circle c({0, 0}, 5);
    cout << "Area: " << c.area() << endl;

    return 0;
}

Enums

#include <iostream>
using namespace std;

// Plain enum (avoid in new code)
enum Color { RED, GREEN, BLUE };

// Scoped enum (C++11, preferred)
enum class Direction { UP, DOWN, LEFT, RIGHT };

int main() {
    Color c = RED;
    Direction d = Direction::UP;

    cout << "Color: " << c << endl;
    cout << "Direction: " << static_cast<int>(d) << endl;

    return 0;
}

Type qualifiers

#include <iostream>
using namespace std;

int main() {
    // const: value cannot change
    const int MAX = 100;
    // MAX = 200; // Error

    // volatile: value may change externally
    volatile int sensor = 0; // Hardware register

    // mutable: can change in const methods
    struct Cache {
        mutable int accessCount = 0;
        int data;
        void access() const { accessCount++; }
    };

    cout << "MAX: " << MAX << endl;
    return 0;
}

Type casting

#include <iostream>
using namespace std;

int main() {
    // C-style cast (avoid)
    int a = 7;
    double b = (double)a / 2;

    // C++ casts (preferred)
    double c = static_cast<double>(a) / 2;
    cout << "static_cast: " << c << endl;

    // const_cast: remove/add const
    const int *p = &a;
    int *q = const_cast<int *>(p);
    *q = 100;
    cout << "Modified: " << a << endl;

    return 0;
}

Mini Practice

Write C++ code that:

  1. Creates variables of each fundamental type and prints their sizes
  2. Uses std::array and iterates with a range-based for loop
  3. Defines a struct with a method
  4. Demonstrates static_cast for type conversion

Up Next

In the next lesson, you'll learn about Operators — arithmetic, comparison, and logical operations.

Related Topics

Frequently Asked Questions about Data Types

What is Data Types in C++?

Data Types is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Data Types?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.

Why is Data Types important in C++?

Data Types is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.