cpp-tipos-de-datos

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

  • 5 min

C++ is a strongly typed and object-oriented language. Therefore, data types determine the characteristics and behavior of the variables that are 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 the size) of each type depend on the compiler and even the operating system.

Basic Types

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 keyword bool and can be assigned a value of true or false, for example:

bool verdadero = true;
bool falso = false;
Copied!

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

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 keyword char and can be assigned a value in single quotes, for example:

char letra = 'A';
Copied!

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

The integer number 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 numeroEntero = 42;
unsigned int numeroEnteroPositivo = 42U;
Copied!

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 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 numeroFloat = 10.5f; // 'f' is optional in C++
double numeroDouble = 20.99;
long double numeroLongDouble = 100.50L; // 'L' is optional in C++
Copied!

Structures

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

Example:

struct Punto {
    int X;
    int Y;
};

Punto punto = {10, 20};
Copied!

Enumerations

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

Example:

enum Dias {
    Lunes, Martes, Miercoles, Jueves, Viernes, Sabado, Domingo
};

Dias hoy = Lunes;
Copied!

Arrays

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

Array example:

int numeros[] = {1, 2, 3, 4, 5};
Copied!

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 store memory addresses and allow direct manipulation of memory.

int numero = 10;
int* puntero = № // Pointer that stores the address of 'numero'
Copied!

References are aliases for other variables.

int numero = 10;
int& referencia = numero; // Reference to 'numero'
Copied!

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

#include <string>

std::string saludo = "Hello, World!";
Copied!

Unions

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

union Datos {
    int entero;
    float flotante;
    char caracter;
};

Datos d;
d.entero = 10;
d.flotante = 20.5f; // The value of entero is no longer valid
Copied!

Classes

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

class Coche {
public:
    std::string marca;
    int año;

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

Coche miCoche;
miCoche.marca = "Toyota";
miCoche.año = 2024;
miCoche.arrancar();
Copied!