Language: EN

cpp-tipos-de-datos

What are data types in C++ and how to use them

C++ is a strongly typed, object-oriented language. Therefore, data types determine the characteristics and behavior of the variables manipulated throughout the execution of a program.

These data types are provided by the language and are designed to handle individual values (such as integers, floating-point numbers, characters, and boolean values).

The implementation details (such as size) of each type depend on the compiler and even the operating system.

Bool Type

The bool type in C++ is a data type used to store logical values (i.e., true or false).

TypeBytesRange
bool1true / false

The bool type is declared using the bool keyword and can be assigned a value of true or false, for example:

bool trueValue = true;
bool falseValue = false;

The bool type is very useful for making decisions in a program and for performing comparisons.

Char Type

The char type in C++ is a data type used to store individual characters (such as letters, numbers, and symbols).

TypeBytesRange
char1

The char type is declared using the char keyword and can be assigned a value in single quotes, for example:

char letter = 'A';

The char type can also be used to store ASCII values, which are numeric codes assigned to each character on the keyboard.

Integers

The integer types in C++ are used to store numbers without a decimal part. They include several variants that differ in size and sign.

TypeBytesRange
signed char1-128 to 127
unsigned char10 to 255
short2-2^15 to 2^15 - 1
unsigned short20 to 2^16 - 1
int4-2^31 to 2^31 - 1
unsigned int40 to 2^32 - 1
long int4/8
long unsigned int4/8
long long int8-2^63 to 2^63 - 1
long long unsigned int80 to 2^64 - 1

Example:

int integerNumber = 42;
unsigned int positiveIntegerNumber = 42U;

2^15 = 32,768 2^16 = 65,536 2^31 = 2,147,483,648 2^32 = 4,294,967,296 2^63 = 9,223,372,036,854,775,808 2^64 = 18,446,744,073,709,551,616

Floating-Point Numbers

The floating-point data types are used to store numbers with decimal parts.

TypeSize (bytes)PrecisionMinimum RangeMaximum Range
float4Single precision±1.5 x 10^-45±3.4 x 10^38
double8Double precision±5.0 x 10^-324±1.7 x 10^308
long double16High precision±3.4 x 10^-4932±1.1 x 10^4932

Example:

float floatNumber = 10.5f; // 'f' is optional in C++
double doubleNumber = 20.99;
long double longDoubleNumber = 100.50L; // 'L' is optional in C++

Structures

struct in C++ allows the creation of user-defined value grouping types.

Example:

struct Point {
    int X;
    int Y;
};

Point point = {10, 20};

Enumerations

enum allows the creation of a set of related constants under a specific type name.

Example:

enum Days {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};

Days today = Monday;

Arrays

arrays allow storing a collection of elements of the same type in a single variable.

Example of an array:

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

Arrays in C++ have a fixed size, and the size must be known at compile time.

You can access elements using indices, starting from 0.

Reference Types

reference types in C++ include pointers and references. These types allow manipulating memory and referring to other variables.

Pointers

Pointers store memory addresses and allow direct manipulation of memory.

Example of a pointer:

int number = 10;
int* pointer = &number; // Pointer that stores the address of 'number'

References

References are aliases for other variables.

Example of a reference:

int number = 10;
int& reference = number; // Reference to 'number'

Strings (std::string)

Strings in C++ are handled through the std::string class, which provides a convenient way to work with sequences of characters.

Example of using std::string:

#include <string>

std::string greeting = "Hello, World!";

Unions

unions allow storing different types of data in the same memory space. Only one of the union members can be used at a time.

union Data {
    int integer;
    float floating;
    char character;
};

Data d;
d.integer = 10;
d.floating = 20.5f; // The value of integer is no longer valid

Classes

classes are groupings that encapsulate data and methods (they are the foundation of object-oriented programming in C++)

class Car {
public:
    std::string brand;
    int year;

    void start() {
        std::cout << "The car is running." << std::endl;
    }
};

Car myCar;
myCar.brand = "Toyota";
myCar.year = 2024;
myCar.start();