WikiGalaxy

Personalize

C++ Data Types

Introduction to Data Types

Data types specify the type of data that a variable can hold in C++. They are essential for memory management and ensuring that operations are performed correctly. C++ supports several data types, including primitive and user-defined types.

Primitive Data Types

Primitive data types are the basic types provided by C++. They include int, char, float, double, and bool. Each type has its own size and range.

User-Defined Data Types

C++ allows users to define their own data types using structures, unions, and classes. These types help in creating complex data structures.

Derived Data Types

Derived data types are based on primitive or user-defined types. They include arrays, pointers, references, and functions.


#include <iostream>
using namespace std;

int main() {
    int age = 25; // Integer data type
    char grade = 'A'; // Character data type
    float height = 5.9; // Float data type
    double distance = 12345.6789; // Double data type
    bool isStudent = true; // Boolean data type

    cout << "Age: " << age << endl;
    cout << "Grade: " << grade << endl;
    cout << "Height: " << height << endl;
    cout << "Distance: " << distance << endl;
    cout << "Is Student: " << isStudent << endl;

    return 0;
}
    

Example Explanation

In this example, we declare variables of different primitive data types and print their values. The int type is used for whole numbers, char for single characters, float and double for floating-point numbers, and bool for boolean values.

Console Output:

Age: 25 Grade: A Height: 5.9 Distance: 12345.6789 Is Student: 1

Integer Data Types

Understanding Integers

Integers are whole numbers without any fractional part. In C++, the int keyword is used to declare integer variables. Integers can be signed or unsigned, with varying sizes like short, long, and long long.


#include <iostream>
using namespace std;

int main() {
    short smallNumber = 32767;
    int regularNumber = 2147483647;
    long largeNumber = 9223372036854775807;
    unsigned int positiveNumber = 4294967295;

    cout << "Short: " << smallNumber << endl;
    cout << "Int: " << regularNumber << endl;
    cout << "Long: " << largeNumber << endl;
    cout << "Unsigned Int: " << positiveNumber << endl;

    return 0;
}
    

Example Explanation

This example demonstrates different integer types in C++. The short type is used for smaller ranges, int for regular ranges, long for larger ranges, and unsigned int for non-negative values only.

Console Output:

Short: 32767 Int: 2147483647 Long: 9223372036854775807 Unsigned Int: 4294967295

Floating Point Data Types

Understanding Floats and Doubles

Floating point data types are used for numbers with decimal points. C++ provides float and double for single and double precision floating point numbers, respectively.


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

int main() {
    float pi = 3.14159f;
    double e = 2.718281828459045;
    long double goldenRatio = 1.618033988749895L;

    cout << fixed << setprecision(10);
    cout << "Float Pi: " << pi << endl;
    cout << "Double E: " << e << endl;
    cout << "Long Double Golden Ratio: " << goldenRatio << endl;

    return 0;
}
    

Example Explanation

This example shows how to use floating point numbers in C++. The float type is used for single precision, double for double precision, and long double for extended precision.

Console Output:

Float Pi: 3.1415901184 Double E: 2.7182818285 Long Double Golden Ratio: 1.6180339887

Character Data Type

Understanding Characters

The char data type is used to store single characters. Characters are stored as integers representing their ASCII values.


#include <iostream>
using namespace std;

int main() {
    char letter = 'C';
    char number = '7';

    cout << "Letter: " << letter << endl;
    cout << "Number as Char: " << number << endl;
    cout << "ASCII of Letter: " << int(letter) << endl;
    cout << "ASCII of Number: " << int(number) << endl;

    return 0;
}
    

Example Explanation

In this example, we demonstrate how to use the char data type. We also convert characters to their ASCII values using type casting.

Console Output:

Letter: C Number as Char: 7 ASCII of Letter: 67 ASCII of Number: 55

Boolean Data Type

Understanding Booleans

The bool data type is used to store boolean values, which can be either true or false. Booleans are often used in control statements.


#include <iostream>
using namespace std;

int main() {
    bool isOpen = true;
    bool isClosed = false;

    cout << "Is Open: " << isOpen << endl;
    cout << "Is Closed: " << isClosed << endl;

    return 0;
}
    

Example Explanation

This example illustrates the use of the bool data type. The output shows boolean values as integers, where true is represented as 1 and false as 0.

Console Output:

Is Open: 1 Is Closed: 0

Arrays as Derived Data Types

Understanding Arrays

Arrays are collections of elements of the same data type. They are used to store multiple values in a single variable, which can be accessed using an index.


#include <iostream>
using namespace std;

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};

    for (int i = 0; i < 5; i++) {
        cout << "Element at index " << i << ": " << numbers[i] << endl;
    }

    return 0;
}
    

Example Explanation

This example demonstrates the use of arrays in C++. We declare an array of integers and access each element using a loop.

Console Output:

Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 Element at index 3: 40 Element at index 4: 50

Pointers as Derived Data Types

Understanding Pointers

Pointers are variables that store memory addresses. They are used for dynamic memory allocation and to access elements in arrays and other data structures.


#include <iostream>
using namespace std;

int main() {
    int value = 42;
    int* pointer = &value;

    cout << "Value: " << value << endl;
    cout << "Pointer Address: " << pointer << endl;
    cout << "Value through Pointer: " << *pointer << endl;

    return 0;
}
    

Example Explanation

This example demonstrates how to use pointers in C++. We declare a pointer to an integer and use it to access the value stored at the memory address it points to.

Console Output:

Value: 42 Pointer Address: 0x7ffee4b7a6fc Value through Pointer: 42

Structures as User-Defined Data Types

Understanding Structures

Structures are used to group different data types together. They are particularly useful for modeling real-world entities with multiple attributes.


#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
    float height;
};

int main() {
    Person person1;
    person1.name = "Alice";
    person1.age = 30;
    person1.height = 5.5;

    cout << "Name: " << person1.name << endl;
    cout << "Age: " << person1.age << endl;
    cout << "Height: " << person1.height << endl;

    return 0;
}
    

Example Explanation

This example shows how to define and use a structure in C++. We create a Person structure with attributes for name, age, and height.

Console Output:

Name: Alice Age: 30 Height: 5.5

Unions as User-Defined Data Types

Understanding Unions

Unions are similar to structures but only one member can be used at a time. They are useful for memory-efficient storage of data.


#include <iostream>
using namespace std;

union Data {
    int intVal;
    float floatVal;
    char charVal;
};

int main() {
    Data data;
    data.intVal = 10;
    cout << "Int Value: " << data.intVal << endl;

    data.floatVal = 220.5;
    cout << "Float Value: " << data.floatVal << endl;

    data.charVal = 'Z';
    cout << "Char Value: " << data.charVal << endl;

    return 0;
}
    

Example Explanation

This example demonstrates how to use a union in C++. We assign different values to a union variable and observe how only the last assigned value is retained.

Console Output:

Int Value: 10 Float Value: 220.5 Char Value: Z

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025