Tamaño de tipos de datos en C
Por
Darío Rivera
Publicado el
en
Lenguaje C
En nuestro artículo anterior vimos todos los Tipos de Datos en C y en especial el tipo de dato char. Cada tipo de dato tiene un almacenamiento específico en bits que puede ser calculado con la función sizeof. Algunos valores pueden variar dependiendo de la máquina en dónde se esté utililizando C.
A continuación se presenta un programa que muestra de manera detallada todos los tamaños de los tipos primitivos.
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main()
{
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| TYPE | SIZE | RANGE \n");
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| char | %d byte | (signed) %d to %d \n", CHAR_BIT/8, CHAR_MIN, CHAR_MAX);
printf("| | | (unsigned) 0 to %d \n", UCHAR_MAX);
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| short | %lu bytes | (signed) %d to %d \n", sizeof(short), SHRT_MIN, SHRT_MAX);
printf("| | | (unsigned) 0 to %d \n", USHRT_MAX);
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| int | %lu bytes | (signed) %d to %d \n", sizeof(int), INT_MIN, INT_MAX);
printf("| | | (unsigned) 0 to %ud \n", UINT_MAX);
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| long | %lu bytes | (signed) %ld to %ld \n", sizeof(long), LONG_MIN, LONG_MAX);
printf("| | | (unsigned) 0 to %lu \n", ULONG_MAX);
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| long | %lu bytes | (signed) %lld to %lld \n", sizeof(long long), LLONG_MIN, LLONG_MAX);
printf("| long | | (unsigned) 0 to %llu \n", ULLONG_MAX);
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| float | %lu bytes | %g to %g \n", sizeof(float), FLT_MIN, FLT_MAX);
printf("| | | %g to %g \n", -FLT_MAX, -FLT_MIN);
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| double | %lu bytes | %g to %g \n", sizeof(double), DBL_MIN, DBL_MAX);
printf("| | | %g to %g \n", -DBL_MAX, -DBL_MIN);
printf("+--------+-----------+------------------------------------------------------+\n");
printf("| long | %lu bytes | %g to %g \n", sizeof(long double), DBL_MIN, DBL_MAX);
printf("| double | | %g to %g \n", -DBL_MAX, -DBL_MIN);
printf("+--------+-----------+------------------------------------------------------+\n");
return 0;
}
Al compilar y ejecutar este programa obtendrás una salida similar a la siguiente:
+--------+-----------+------------------------------------------------------+
| TYPE | SIZE | RANGE
+--------+-----------+------------------------------------------------------+
| char | 1 byte | (signed) -128 to 127
| | | (unsigned) 0 to 255
+--------+-----------+------------------------------------------------------+
| short | 2 bytes | (signed) -32768 to 32767
| | | (unsigned) 0 to 65535
+--------+-----------+------------------------------------------------------+
| int | 4 bytes | (signed) -2147483648 to 2147483647
| | | (unsigned) 0 to 4294967295d
+--------+-----------+------------------------------------------------------+
| long | 8 bytes | (signed) -9223372036854775808 to 9223372036854775807
| | | (unsigned) 0 to 18446744073709551615
+--------+-----------+------------------------------------------------------+
| long | 8 bytes | (signed) -9223372036854775808 to 9223372036854775807
| long | | (unsigned) 0 to 18446744073709551615
+--------+-----------+------------------------------------------------------+
| float | 4 bytes | 1.17549e-38 to 3.40282e+38
| | | -3.40282e+38 to -1.17549e-38
+--------+-----------+------------------------------------------------------+
| double | 8 bytes | 2.22507e-308 to 1.79769e+308
| | | -1.79769e+308 to -2.22507e-308
+--------+-----------+------------------------------------------------------+
| long | 16 bytes | 2.22507e-308 to 1.79769e+308
| double | | -1.79769e+308 to -2.22507e-308
+--------+-----------+------------------------------------------------------+