The numbers in are stored in binary format. Thus number 10 becomes 1010
and number 22% is 10110
.
In C ++ the types do not have a fixed size, their size is dependent on the architecture of the system. Of course, for a given architecture the size of a type is fixed.
In the case of int
, in an application for pc it usually occupies 32 bits. If we assume that this is your case and given that 1 bit is used to indicate the sign of the number we have that the range of values that the type int
admits is 2 ^ 31 a (-2 ^ 31) -1 or, said in another way from -2,147,483,648 to 2,147,483,647. This means that any number within this range will be stored in 4 bits regardless of its value. Thus the value 1
and the value 123456789
will occupy both 32 bits (the digits of greater weight that are not in use are initialized with zeros).
In any case, C ++ has a series of utilities in the limits
library that inform you about the valid range for each type of data. So:
#include <limits>
#include <iostream>
int main()
{
std::cout << "rango de int: " << std::numeric_limits<int>::min() << " a "
<< std::numeric_limits<int>::max() << '\n'
<< "rango de short: " << std::numeric_limits<short>::min() << " a "
<< std::numeric_limits<short>::max() << '\n'
<< "rango de char: " << (int)std::numeric_limits<char>::min() << " a "
<< (int)std::numeric_limits<char>::max() << '\n';
}
Your exit:
rango de int: -2147483648 a 2147483647
rango de short: -32768 a 32767
rango de char: -128 a 127
Exceeding the capacity of one of the types causes an effect known as overflow ( overflow ). In this case, a specific flag is activated in the processor and the number loses values:
int main()
{
int valor = std::numeric_limits<int>::max();
std::cout << valor << '\n' << valor + 1;
}
Your exit:
2147483647
-2147483648
As you can see, the number has suddenly turned around. Go from a positive number to a negative number this is because:
0111 1111 1111 1111 1111 1111 1111 1111 -> 2147483647
+ 1
---------------------------------------
1000 0000 0000 0000 0000 0000 0000 0000 -> -2147483648
^
Este es el bit de signo
Another example:
int main()
{
int valor = std::numeric_limits<int>::max();
std::cout << valor << '\n' << valor + valor;
}
Exit:
2147483647
-2
Explanation to this second case
0111 1111 1111 1111 1111 1111 1111 1111 -> 2147483647
+ 0111 1111 1111 1111 1111 1111 1111 1111 -> 2147483647
---------------------------------------
1 1111 1111 1111 1111 1111 1111 1111 1110 -> -2
^ ^
Este dígito se pierde Los números negativos se almacenan en complemento a 2
Thus, any sum of two integers is always going to give a result, even if this is incorrect (that's what the overflow flag is for).
What happens in the case of character strings?
Character strings are not a native type. They do not occupy a regular number of bytes since it depends on the size of the text to be stored.
Here we should distinguish between character arrays and string classes ( std::string
, std::wstring
, ...):
-
In the case of character arrays, you must bear in mind that at some point you have indicated the maximum size. In this case, if you try to write beyond the defined size (remember that the indexes begin to count at 0), what you will get is to write in memory that does not belong to the array. This usually results in a malfunction of the program or even in a casque if the Operating System realizes.
char cadena[10];
cadena[10] = '0'; // Error estás escribiendo fuera del arreglo
-
In the case of string classes you should know that it is the classes themselves that are responsible for managing the memory for the string. They alone are able to adapt the size of the buffer so that the text you try to store enters. This type of elements also has a maximum size that can be around 2 ^ 32 or 2 ^ 64 characters (that is if you do not run out of memory before).