Instagram
youtube
Facebook
Twitter

C++ Datatypes

Data Types in C++

In C++, there are several built-in data types you can use to declare variables and store different kinds of data. They are some of the most commonly used data types in C++.

1 Fundamental Data Types:

  • bool: That Represents a boolean value,either true or false.
#include <iostream>

using namespace std;

int main() {

    bool boolVar = true;

    cout << "Boolean: " << boolVar << endl;

    return 0;
}

 

  • char: Stores a single character.
#include <iostream>

using namespace std;

int main() {

    char charVar = 'A';

    cout << "Character: " << charVar << endl;

    return 0;

}

 

  • int: Is used for storing integers (whole numbers).
#include <iostream>

using namespace std;

int main() {

    int integerVar = 10;

    short shortVar = 5;

    long long Var = 1000000;

    long long longLongVar = 123456789123;

    cout << "Integer: " << integerVar << endl;

    cout << "Short: " << shortVar << endl;

    cout << "Long: " << long Var << endl;

    cout << "Long Long: " << longLongVar << endl;

    return 0;

}

 

  • float: Represents single-precision floating-point numbers.
#include <iostream>

using namespace std;

int main() {

    float floatVar = 3.14;

    double doubleVar = 2.71828;

    long double longDoubleVar = 0.12345678901234567890;

    cout << "Float: " << floatVar << endl;

    cout << "Double: " << doubleVar << endl;

    cout << "Long Double: " << longDoubleVar << endl;

    return 0;

}

 

  • String Data Type: The string data type is used to represent sequences of characters, such as text, within a programming language, allowing manipulation and storage of textual information.
#include <iostream>

using namespace std;

int main() {

    string strVar = "Hello, C++!";

    cout << "String: " << strVar << endl;

    return 0;

}

 

  • Array Data Type:An array is a linear data structure that stores a fixed-size collection of elements of the same data type, accessible using an index or position.
#include <iostream>

using namespace std;

int main() {

    int intArray[5] = {1, 2, 3, 4, 5};

    cout << "Array Element 3: " << intArray[2] << endl;

    return 0;

}

 

  • double: Stores double-precision floating-point numbers with more precision than float.
#include <iostream>

int main() {

    double num = 3.14159;

    std::cout << "Value of num: " << num << std::endl;

    return 0;

}

 

  • void: Represents the absence of a type. It’s generally used as a return type for functions that don’t return any value.
#include <iostream>

void myFunction() {

    std::cout << "Hello, World!" << std::endl;
}

int main() {

    myFunction();

    return 0;

}

 

2 Modifiers:

  • signed: It can be used with integer types to store both positive and negative values (the default behavior for int).
#include <iostream>

int main() {

    signed int signedInt = -42;

    signed char signedChar = -127;

    std::cout << "Signed Int: " << signedInt << std::endl;

    std::cout << "Signed Char: " << signedChar << std::endl;

    return 0;

}

 

  • unsigned: Can be used with integer types to allow them to store only non-negative values.
#include <iostream>

int main() {

    int signedVar = -10;

    unsigned int unsignedVar = 10;

    std::cout << "Signed variable: " << signedVar << std::endl;

    std::cout << "Unsigned variable: " << unsignedVar << std::endl;

    return 0;

}

 

  • short: Represents a shorter range of values for integers.
#include <iostream>

int main() {

    short smallNumber = 1234;

    unsigned short positiveSmallNumber = 5678;

    short int anotherSmallNumber = -9876;

    std::cout << "Small Number: " << smallNumber << std::endl;

    std::cout << "Positive Small Number: " << positiveSmallNumber << std::endl;

    std::cout << "Another Small Number: " << anotherSmallNumber << std::endl;

    return 0;

}

 

  • long: Represents a longer range of values for integers.
#include <iostream>

int main() {

    long a = 1234567890 L;              

    unsigned long b = 987654321 UL;          

    std::cout << "a: " << a << std::endl;

    std::cout << "b: " << c << std::endl;

    return 0;

}

 

  • long long: Represents an even longer range of values for integers.
#include <iostream>

int main() {

    long a = 1234567890 L;                

    long long b = 123456789012345 LL;     

    unsigned long c = 987654321 UL;       

    unsigned long long d = 9876543210123456789 ULL; 

    std::cout << "a: " << a << std::endl;

    std::cout << "b: " << b << std::endl;

    std::cout << "c: " << c << std::endl;

    std::cout << "d: " << d << std::endl;

    return 0;

}

 

3 Derived Data Types:

  • Arrays: A collection of elements of the same type stored in conterminous memory locations.
#include <iostream>

#include <string>

struct Person {

    std::string name;

    int age;

};

int main() {

    const int numPeople = 3;

    Person people[numPeople];

    people[0] = {"Alice", 25};

    people[1] = {"Bob", 30};

    people[2] = {"Charlie", 22};

    for (int i = 0; i < numPeople; ++i) {

        std::cout << "Name: " << people[i].name << ", Age: " << people[i].age << std::endl;

    }

    return 0;

}

 

  • Pointers: Pointers are variables that store the memory addresses of other variables.
#include <iostream>

#include <string>

struct Student {

    std::string name;

    int rollNumber;

};

int main() {

    Student students[3];

    students[0] = {"John", 101};

    students[1] = {"Emily", 102};

    students[2] = {"Michael", 103};

    Student *ptr = students;

    for (int i = 0; i < 3; ++i) {

        std::cout << "Student " << i + 1 << ": ";

        std::cout << "Name: " << (ptr + i)->name << ", ";

        std::cout << "Roll Number: " << (ptr + i)->rollNumber << std::endl;

    }

    return 0;

}

 

  • References: An alias for an existing variable
#include <iostream>

#include <string>

class Student {

public:

    Student(const std::string& name) : name(name) {}

    void display() const {

        std::cout << "Student Name: " << name << std::endl;

    }

private:

    std::string name;

};

int main() {

    Student student("Bob");

    Student & ref = student;  // Reference to a Student object

    ref.display();  // Output: Student Name: Bob 

    return 0;

}

 

  • Structures: They allow you to define your own data types by combining different types into a single entity.
#include <iostream>

#include <string>

struct Person {

    std::string name;

    int age;

    char gender;

};

int main() {

    Person person1;

    person1.name = "Alice";

    person1.age = 25;

    person1.gender = 'F';

    std::cout << "Name: " << person1.name << std::endl;

    std::cout << "Age: " << person1.age << std::endl;

    std::cout << "Gender: " << person1.gender << std::endl;

    return 0;

}

 

  • Enumerations (enums): Represent a set of named values.
#include <iostream>

enum Day {

    Sunday,

    Monday,

    Tuesday,

    Wednesday,

    Thursday,

    Friday,

    Saturday

};

int main() {

    Day today = Wednesday;

    switch (today) {

        case Sunday:

            std::cout << "It's a relaxing day." << std::endl;

            break;

        case Monday:

            std::cout << "Back to work." << std::endl;

            break;

        case Wednesday:

            std::cout << "Halfway through the week." << std::endl;

            break;

        default:

            std::cout << "Just another day." << std::endl;

            break;
    }

    return 0;

}

 

4 Other Data Types:

  • String( from the Standard Library): Represents a sequence of characters.
#include <iostream>

#include <string>

int main() {

    std::string str1 = "Hello, ";

    std::string str2 = "world!";

    std::string result = str1 + str2; // Concatenation

    std::cout << result << std::endl;

    return 0;

}

 

  • Class( from Object-Oriented Programming): Allows you to create your types by defining a design for objects.
#include <iostream>

class Rectangle {

private:

    int width;

    int height;

public:

    Rectangle(int w, int h) : width(w), height(h) {}

    int calculateArea() {

        return width * height;
    }
};

int main() {

    Rectangle myRectangle(5, 7);

    std::cout << "Area: " << myRectangle.calculateArea() << std::endl;

    return 0;

}

 

  • Union: A union is analogous to a structure but allows different members to share the same memory space.
#include <iostream>

union Data {

    int iValue;

    double dValue;

    char cValue;

};

int main() {

    Data myData;

    myData.iValue = 42;

    std::cout << "Integer Value: " << myData.iValue << std::endl;

    myData.dValue = 3.14;

    std::cout << "Double Value: " << myData.dValue << std::endl;

    myData.cValue = 'A';

    std::cout << "Character Value: " << myData.cValue << std::endl;

    return 0;

}

 

These are the introductory data types in C++. Still, C++ also allows you to define your custom data types using classes and structures, which is a fundamental aspect of Object-Oriented Programming.