La mayoría de las veces en la programación competitiva existe la necesidad de asignar a la variable el valor máximo o mínimo que el tipo de datos puede contener, pero recordar un número tan grande y preciso resulta ser un trabajo difícil. Por lo tanto, C++ tiene ciertas macros para representar estos números, de modo que puedan asignarse directamente a la variable sin tener que escribir el número entero.
El
Echemos un vistazo a un ejemplo:
C++#include // for int char macros #include // for float double macros #include using namespace std; int main() { // Displaying ranges with the help of macros cout << 'char ranges from: ' << CHAR_MIN << ' to ' << CHAR_MAX << endl; cout << 'nnshort int ranges from: ' << SHRT_MIN << ' to ' << SHRT_MAX << endl; cout << 'nint ranges from: ' << INT_MIN << ' to ' << INT_MAX << endl; cout << 'nlong int ranges from: ' << LONG_MIN << ' to ' << LONG_MAX << endl; cout << 'nfloat ranges from: ' << FLT_MIN << ' to ' << FLT_MAX << endl; return 0; }
Producción
Python imprime con 2 decimales
char ranges from: -128 to 127
nshort int ranges from: -32768 to 32767
int ranges from: -2147483648 to 2147483647
long int ranges from: -9223372036854775808 to 9223372036854775807
float ranges from: 1.17549e-38 to 3.40282e+38
Tipos de datos y sus macros de rango
A continuación se menciona una lista de algunas de las macros de tipos de datos:
Tipo de datos | Rango | Macro para valor mínimo | Macro para valor máximo |
---|---|---|---|
carbonizarse | -128 a +127 | Char_min | CHAR_MAX |
carácter corto | -128 a +127 | SCHAR_MIN | SCHAR_MAX |
carácter sin firmar | 0 a 255 | -- | volando_max |
corto int | -32768 a +32767 | SHRT_MIN | SHRT_MAX |
int corto sin firmar fila y columna | 0 a 65535 | -- | USHRT_MAX |
entero | -2147483648 a +2147483647 | INT_MIN | INT_MAX |
entero sin firmar | 0 al 4294967295 | -- | UINT_MAX |
largo int | -9223372036854775808 a +9223372036854775807 | LONG_MIN | LARGO_MAX |
int largo sin firmar | 0 a 18446744073709551615 | -- | usong_max |
largo largo int | -9223372036854775808 a +9223372036854775807 | barco_min | LLONG_MAX cena versus cena |
int largo largo sin firmar | 0 a 18446744073709551615 | -- | ULLONG_MAX |
flotar | 1.17549e-38 a 3.40282e+38 | Flt_min | FLT_MAX |
flotador (negativo) | -1.17549e-38 a -3.40282e+38 | -Lt_min | -FLT_MAX |
doble | 2.22507e-308 a 1.79769e+308 | DBL_MIN | DBL_MAX |
doble (negativo) | -2.22507e-308 a -1.79769e+308 | -DBL_MIN | -DBL_MAX |
Límites de tipos de datos en C++ moderno
El enfoque macro anterior para los límites superior e inferior del tipo de datos es el antiguo enfoque del lenguaje C heredado de C++. Pero C++ también tiene su propio método para proporcionar a los programadores la misma información.
C++ ofrece la límites_numéricos<> plantilla de clase como una alternativa moderna a estas macros. Esta plantilla proporciona un enfoque más orientado a objetos para acceder a los límites de tipos de datos. Se define dentro del
Echemos un vistazo a un ejemplo:
C++#include #include using namespace std; int main() { // Displaying ranges with the help of macros cout << 'short int ranges from: ' << numeric_limits<short int>::min() << ' to ' << numeric_limits<short int>::max() << endl; cout << 'nint ranges from: ' << numeric_limits<int>::min() << ' to ' << numeric_limits<int>::max() << endl; cout << 'nlong int ranges from: ' << numeric_limits<long>::min() << ' to ' << numeric_limits<long>::max() << endl; cout << 'nfloat ranges from: ' << numeric_limits<float>::min() << ' to ' << numeric_limits<float>::max() << endl; return 0; }
Producción
short int ranges from: -32768 to 32767
int ranges from: -2147483648 to 2147483647
long int ranges from: -9223372036854775808 to 9223372036854775807
float ranges from: 1.17549e-38 to 3.40282e+38
Se recomienda utilizar este enfoque para encontrar los límites superior e inferior del tipo de datos en lugar de macros, ya que es más seguro y legible en comparación con el enfoque basado en macros.