Geek Slack

Learn C++
About Lesson


C++ Data Types


C++ Data Types

Data types in C++ specify the type of data that variables can hold. C++ provides several basic data types, which can be broadly categorized into:

  • Primitive Data Types: Integer, Floating-Point, Character, Boolean.
  • Derived Data Types: Arrays, Pointers, References, Functions, Enumerations, Structures, and Unions.

1. Primitive Data Types

Primitive data types represent the basic building blocks of data manipulation in C++. They include:

  • int: Integer data type for whole numbers.
  • float: Floating-point data type for single-precision floating-point numbers.
  • double: Floating-point data type for double-precision floating-point numbers.
  • char: Character data type for single characters.
  • bool: Boolean data type representing true or false values.

Example:

#include <iostream>

int main() {
    int myInt = 10;
    float myFloat = 3.14f;
    double myDouble = 3.14159;
    char myChar = 'A';
    bool myBool = true;

    std::cout << "Integer: " << myInt << std::endl;
    std::cout << "Float: " << myFloat << std::endl;
    std::cout << "Double: " << myDouble << std::endl;
    std::cout << "Character: " << myChar << std::endl;
    std::cout << "Boolean: " << myBool << std::endl;

    return 0;
}

2. Modifiers for Primitive Data Types

C++ also provides modifiers for these data types to adjust their range or precision:

  • short: Shortens the range of integers.
  • long: Increases the range of integers.
  • long long: Provides even larger range for integers.

Example:

#include <iostream>

int main() {
    short shortInt = 10;
    long long longLongInt = 123456789012345;

    std::cout << "Short Integer: " << shortInt << std::endl;
    std::cout << "Long Long Integer: " << longLongInt << std::endl;

    return 0;
}

3. Derived Data Types

Derived data types are constructed using primitive data types and other derived types. They include:

  • Arrays: Collection of elements of the same data type.
  • Pointers: Variables that store memory addresses.
  • References: Alias to an existing variable.
  • Structures: Groups different data types together under a single name.
  • Unions: Similar to structures but allocate enough memory for only one member at a time.
  • Enumerations (enums): User-defined data type consisting of a set of named constants.
  • Functions: Group of statements that perform a task.

Example:

#include <iostream>

// Structure example
struct Person {
    std::string name;
    int age;
};

int main() {
    int numbers[5] = {1, 2, 3, 4, 5}; // Array of integers
    int* ptr = &numbers[0]; // Pointer to an integer
    int& ref = numbers[1]; // Reference to an integer

    Person person1 = {"Alice", 25}; // Structure instance

    std::cout << "Array Element: " << *ptr << std::endl;
    std::cout << "Reference Value: " << ref << std::endl;
    std::cout << "Structure: " << person1.name << " (" << person1.age << " years old)" << std::endl;

    return 0;
}

Conclusion

Understanding data types in C++ is crucial for effectively manipulating data in programs. This chapter covered primitive data types such as integers, floating-point numbers, characters, and booleans, as well as derived data types including arrays, pointers, structures, unions, enumerations, and functions. Mastery of these concepts will enable you to write efficient and structured C++ programs.

Join the conversation