Tipos de datos en C

Author
By Darío Rivera
Posted on 2021-02-02 in Lenguaje C

Los tipos de datos en un lenguaje de programación son todos aquellos tipos de valores que una variable puede representar o que una función puede devolver o aceptar como argumentos. En C existen los siguientes tipos de datos:

- Básicos
- Enumerados
- El tipo void
- Derivados

A continuación tienes una lista de los tipos en C, su tamaño y rango.

TipoDescripciónTamañoRango
char Single character 1 byte (signed) -128 to 127
(unsigned) 0 to 255
short Whole numbers
(small range)
2 byte (signed) -32,768 to 32,767
(unsigned) 0 to 65,535
int Whole numbers 2 byte (signed) -32,768 to 32,767
(unsigned) 0 to 65,535
4 byte (signed) -2,147,483,648 to 2,147,483,647
(unsigned) 0 to 4,294,967,295
long Whole numbers
(bigger range)
4 byte (signed) -2,147,483,648 to 2,147,483,647
(unsigned) 0 to 4,294,967,295
float Floating-point numbers
(6 decimal places)
4 byte +/-3.4 x 10^38
double Double precision (more accurate) floating-point numbers
(15 decimal places)
8 byte +/-1.7 x 10^308
long double Double precision (more accurate) floating-point numbers
(19 decimal places)
10 byte -3.4 x 10^4932 to 1.1 x 10^4932
pointer 2 byte near, _cs, _ds, _es, _ss
4 byte far, huge

Algunos valores pueden variar dependiendo de la máquina en dónde estés desarrollando. A continuación te dejo un sencillo programa en C que verifica los tamaños y rangos de los diferentes tipos de datos.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main()
{
    printf("+--------+--------------+------------------------------------------------------+\n");
    printf("|  TYPE  | SIZE (bytes) |           RANGE                                       \n");
    printf("+--------+--------------+------------------------------------------------------+\n");
    printf("| char   |      %d       | (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       | (signed) %d to %d     \n", sizeof(short), SHRT_MIN, SHRT_MAX);
    printf("|        |              | (unsigned) 0 to %d    \n", USHRT_MAX);
    printf("+--------+--------------+------------------------------------------------------+\n");
    printf("| int    |      %lu       | (signed) %d to %d     \n", sizeof(int), INT_MIN, INT_MAX);
    printf("|        |              | (unsigned) 0 to %ud    \n", UINT_MAX);
    printf("+--------+--------------+------------------------------------------------------+\n");
    printf("| long   |      %lu       | (signed) %ld to %ld     \n", sizeof(long), LONG_MIN, LONG_MAX);
    printf("|        |              | (unsigned) 0 to %lu     \n", ULONG_MAX);
    printf("+--------+--------------+------------------------------------------------------+\n");
    printf("| float  |      %lu       | %g to %g     \n", sizeof(float), FLT_MIN, FLT_MAX);
    printf("|        |              | %g to %g     \n", -FLT_MAX, -FLT_MIN);
    printf("+--------+--------------+------------------------------------------------------+\n");
    printf("| double |      %lu       | %g to %g     \n", sizeof(double), DBL_MIN, DBL_MAX);
    printf("|        |              | %g to %g     \n", -DBL_MAX, -DBL_MIN);
    printf("+--------+--------------+------------------------------------------------------+\n");
    printf("| long   |      %lu      | %g to %g     \n", sizeof(long double), DBL_MIN, DBL_MAX);
    printf("| double |              | %g to %g     \n", -DBL_MAX, -DBL_MIN);
    printf("+--------+--------------+------------------------------------------------------+\n");
}

Al ejecutar este script obtendrás una salida similar a la siguiente:

+--------+--------------+------------------------------------------------------+
|  TYPE  | SIZE (bytes) |           RANGE                                       
+--------+--------------+------------------------------------------------------+
| char   |      1       | (signed) -128 to 127     
|        |              | (unsigned) 0 to 255    
+--------+--------------+------------------------------------------------------+
| short  |      2       | (signed) -32768 to 32767     
|        |              | (unsigned) 0 to 65535    
+--------+--------------+------------------------------------------------------+
| int    |      4       | (signed) -2147483648 to 2147483647     
|        |              | (unsigned) 0 to 4294967295d    
+--------+--------------+------------------------------------------------------+
| long   |      8       | (signed) -9223372036854775808 to 9223372036854775807     
|        |              | (unsigned) 0 to 18446744073709551615     
+--------+--------------+------------------------------------------------------+
| float  |      4       | 1.17549e-38 to 3.40282e+38     
|        |              | -3.40282e+38 to -1.17549e-38     
+--------+--------------+------------------------------------------------------+
| double |      8       | 2.22507e-308 to 1.79769e+308     
|        |              | -1.79769e+308 to -2.22507e-308     
+--------+--------------+------------------------------------------------------+
| long   |      16      | 2.22507e-308 to 1.79769e+308     
| double |              | -1.79769e+308 to -2.22507e-308     
+--------+--------------+------------------------------------------------------+

Si te ha gustado este artículo puedes invitarme a tomar una taza de café


Acerca de Darío Rivera

Author

Ingeniero de desarrollo en PlacetoPay , Medellín. Darío ha trabajado por más de 6 años en lenguajes de programación web especialmente en PHP. Creador del microframework DronePHP basado en Zend y Laravel.

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.