Título: Poniendo a prueba los tipos de variables en c
Publicado por: Nucleorion en 2 Agosto 2016, 12:25 pm
Hola, Estoy haciendo un programa para comprobar el tamaño y signo admitido por las variables en c y su funcionalidad en cada version de windows y arquitectura. Todo empezo porque cuando intento usar por ejemplo double se queda calculando indefinidamente. Este es el programa: #include <stdio.h> #include <stdlib.h> void main(){ char tipo_char=1, tipo_char_max=1; // -128 a 128 unsigned char tipo_unsigned_char=1, tipo_unsigned_char_max=1; // 0 a 256 short int tipo_short_int=1, tipo_short_int_max=1; // -32768 a 32767 unsigned short int tipo_unsigned_short_int=1, tipo_unsigned_short_int_max=1; // 0 a 65 535 long int tipo_long_int=1, tipo_long_int_max=1; //-2 147 483 648 a 2 147 483 647 unsigned long int tipo_unsigned_long_int=1, tipo_unsigned_long_int_max=1; // 0 a 4 294 967 295 int tipo_int=1, tipo_int_max=1; //-2 147 483 648 a 2 147 483 647 unsigned int tipo_unsigned_int=1, tipo_unsigned_int_max=1; // 0 a 4 294 967 295 double tipo_double=1, tipo_double_max=1; // 18 446 744 073 709 551 616 float tipo_float=1, tipo_float_max=1; // 18 446 744 073 709 551 616 while(tipo_char>0){ tipo_char_max=tipo_char+(tipo_char-1); tipo_char=tipo_char*2; //printf("La variable char admite como maximo: %i \n\n",tipo_char); } printf("La variable char admite numeros entre: %i y %i \n\n",tipo_char,tipo_char_max); while(tipo_unsigned_char>0){ tipo_unsigned_char_max=tipo_unsigned_char+(tipo_unsigned_char-1); tipo_unsigned_char=tipo_unsigned_char*2; } printf("La variable unsigned char admite numeros entre: %i y %i \n\n",tipo_unsigned_char,tipo_unsigned_char_max); while(tipo_short_int>0){ tipo_short_int_max=tipo_short_int+(tipo_short_int-1); tipo_short_int=tipo_short_int*2; } printf("La variable short int admite numeros entre: %i y %i \n\n",tipo_short_int,tipo_short_int_max); while(tipo_unsigned_short_int>0){ tipo_unsigned_short_int_max=tipo_unsigned_short_int+(tipo_unsigned_short_int-1); tipo_unsigned_short_int=tipo_unsigned_short_int*2; //printf("La variable admite como maximo: %i \n\n",tipo_unsigned_short_int); } printf("La variable unsigned short int admite numeros entre: %i y %i \n\n",tipo_unsigned_short_int,tipo_unsigned_short_int_max); while(tipo_long_int>0){ tipo_long_int_max=tipo_long_int+(tipo_long_int-1); tipo_long_int=tipo_long_int*2; //printf("La variable admite como maximo: %i \n\n",tipo_long_int); } printf("La variable long int admite numeros entre: %i y %i \n\n",tipo_long_int,tipo_long_int_max); while(tipo_unsigned_long_int>0){ tipo_unsigned_long_int_max=tipo_unsigned_long_int+(tipo_unsigned_long_int-1); tipo_unsigned_long_int=tipo_unsigned_long_int*2; //printf("La variable admite como maximo: %i \n\n",tipo_unsigned_long_int); } printf("La variable unsigned long int admite numeros entre: %u y %u \n\n",tipo_unsigned_long_int,tipo_unsigned_long_int_max); while(tipo_int>0){ tipo_int_max=tipo_int+(tipo_int-1); tipo_int=tipo_int*2; //printf("La variable admite como maximo: %i \n\n",tipo_long_int); } printf("La variable int admite numeros entre: %i y %i \n\n",tipo_int,tipo_int_max); while(tipo_unsigned_int>0){ tipo_unsigned_int_max=tipo_unsigned_int+(tipo_unsigned_int-1); tipo_unsigned_int=tipo_unsigned_int*2; //printf("La variable admite como maximo: %i \n\n",tipo_unsigned_int); } printf("La variable unsigned int admite numeros entre: %u y %u \n\n",tipo_unsigned_int,tipo_unsigned_int_max); while(tipo_float>0){ tipo_float_max=tipo_float+(tipo_float-1); tipo_float=tipo_float*2; //printf("La variable admite como maximo: %i \n\n",tipo_float); } //printf("La variable float admite numeros entre: %4.2f y %4.2f \n\n",tipo_float,tipo_float_max); while(tipo_double>0){ tipo_double_max=tipo_double+(tipo_double-1); tipo_double=tipo_double*2; //printf("La variable admite como maximo: %i \n\n",tipo_double); } //tipo_double=18446744073709551616; //printf("La variable double admite numeros entre: %4.2f y %4.2f \n\n",tipo_double,tipo_double_max); system("pause"); }
Y este el resultado: La variable char admite numeros entre: -128 y 127
La variable unsigned char admite numeros entre: 0 y 255
La variable short int admite numeros entre: -32768 y 32767
La variable unsigned short int admite numeros entre: 0 y 65535
La variable long int admite numeros entre: -2147483648 y 2147483647
La variable unsigned long int admite numeros entre: 0 y 4294967295
La variable int admite numeros entre: -2147483648 y 2147483647
La variable unsigned int admite numeros entre: 0 y 4294967295
_
Y tanto el tipo double como float se queda ahi trabajando pero no termina ni en varios minutos. El ordenador es un i5, 8GB de RAM. El SO Win7 64bits. El compilador minGW64 gcc.exe. El editor Notepad++. Que puede fallar para que double y float no me den el resultado esperado?
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: Nucleorion en 2 Agosto 2016, 12:31 pm
Vale ya he visto que tengo mal el formato en los printf. Voy a revisarlo
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: AlbertoBSD en 2 Agosto 2016, 12:35 pm
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: Nucleorion en 2 Agosto 2016, 13:13 pm
No, no termina. Nunca sale lo de pulse una tecla para continuar.
Edito el post inicial ya que unsigned long int y unsigned int, fallaban porque ponia %i en vez de %u
En cuanto a los dos ultimos tipos double y float aunque comente el printf se quedan atascados supuestamente calculando y consumiendo el 25% del procesador cuatro nucleos.
Que hago mal para que no termine de multiplicarse por dos. double y float no se resetean como los otros tipos cuando se desbordan?
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: ivancea96 en 2 Agosto 2016, 13:36 pm
Cuando un float, double o long double llega al límite, se establece a un valor que se trata como "infinito". Mientras que nos números enteros se manejan directamente como están en la memoria, los float tienen un formato especial. http://en.cppreference.com/w/c/types/limits (http://en.cppreference.com/w/c/types/limits)
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: Nucleorion en 2 Agosto 2016, 19:11 pm
infinito, que bien!! infinito es mayor que cero!! asi que no acababa nunca XD
Hay alguna manera de saber cuando se ha desbordado un double y un float? Es decir, no quiero usar una constante para ver si se llega al final. Quiero desbordar el tipo detectarlo, e imprimir su valor mayor y menor, igual que hago en los tipos mas pequeños.
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: ivancea96 en 2 Agosto 2016, 21:32 pm
Si pones 1.0/0.0, obtedrás el valor infinito. Cuidado de no poner 1/0, ya que estos son enteros, y tu programa tendrá un error de división por cero.
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: Nucleorion en 3 Agosto 2016, 17:36 pm
Funciona lo de dividir por cero para comparar con el infinito!!
Dejando los decimales aparte, que veo que tienen tela. me quedan unas dudas.
int, long, y long int es lo mismo? o tengo algo mal?
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: AlbertoBSD en 4 Agosto 2016, 01:34 am
Actualmente en la mayoria de los sistemas de 32 bits tienen dicho espacio de almacenamiento, la diferencia radicaba en aequitecturas de de 16 bits donde un int tenia espacio para 16 bits y un long era el doble de esto.
http://stackoverflow.com/questions/900230/difference-between-long-and-int-data-types
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: Nucleorion en 5 Agosto 2016, 11:47 am
Y en mi caso que el sistema es de 64 bits? realmente se puede manejar un bloque de 32 bits?
Que hace, optimizar y guardar en memoria dos de 32 juntos, o que?
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: ivancea96 en 5 Agosto 2016, 13:18 pm
Generalmente: char 1, short 2, int 4, long long 8. Si quieres verdadera precisión al conocer el tamaño de las variables, tienes: int8_t - uint8_t int16_t - uint16_t int32_t - uint32_t int64_t - uint64_t
Más información de los tipos en: http://www.cplusplus.com/reference/cstdint/ (http://www.cplusplus.com/reference/cstdint/) Esos están asegurados de tener ese tamaño. Son todos tipos enteros, con o sin signo (según tengan la 'u' delante)
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: Nucleorion en 26 Septiembre 2016, 18:18 pm
Joer, no vi el cambio de pagina. Muchas gracias. Intente usar ese tipo de declaracion pero no me funcionó. Volveré a probar. El programa quedo asi: #include <stdio.h> #include <stdlib.h> #include <limits.h> void main(){ char tipo_char=1, tipo_char_max=1; // -128 a 128 unsigned char tipo_unsigned_char=1, tipo_unsigned_char_max=1; // 0 a 256 short int tipo_short_int=1, tipo_short_int_max=1; // -32768 a 32767 unsigned short int tipo_unsigned_short_int=1, tipo_unsigned_short_int_max=1; // 0 a 65 535 int tipo_int=1, tipo_int_max=1; //-2 147 483 648 a 2 147 483 647 long tipo_long=1, tipo_long_max=1; //-2 147 483 648 a 2 147 483 647 long int tipo_long_int=1, tipo_long_int_max=1; //-2 147 483 648 a 2 147 483 647 unsigned int tipo_unsigned_int=1, tipo_unsigned_int_max=1; // 0 a 4 294 967 295 unsigned long tipo_unsigned_long=1, tipo_unsigned_long_max=1; // 0 a 4 294 967 295 unsigned long int tipo_unsigned_long_int=1, tipo_unsigned_long_int_max=1; // 0 a 4 294 967 295 long long tipo_long_long=1, tipo_long_long_max=1; // -9223372036854775808 a 9223372036854775807 unsigned long long tipo_unsigned_long_long=1, tipo_unsigned_long_long_max=1; // 0 a 18 446 744 073 709 551 616 int bits=1; while(tipo_char>0){ tipo_char_max=tipo_char+(tipo_char-1); tipo_char=tipo_char*2; //printf("char admite como maximo: %i \n\n",tipo_char); bits++; } printf("char ................. %ibits de %i a %i \n\n",bits,tipo_char,tipo_char_max); bits=0; while(tipo_unsigned_char>0){ tipo_unsigned_char_max=tipo_unsigned_char+(tipo_unsigned_char-1); tipo_unsigned_char=tipo_unsigned_char*2; bits++; } printf("unsigned char ........ %ibits .. de %i a %i \n\n",bits,tipo_unsigned_char,tipo_unsigned_char_max); bits=1; while(tipo_short_int>0){ tipo_short_int_max=tipo_short_int+(tipo_short_int-1); tipo_short_int=tipo_short_int*2; bits++; } printf("short int ........... %ibits .. de %i a %i \n\n",bits,tipo_short_int,tipo_short_int_max); bits=0; while(tipo_unsigned_short_int>0){ tipo_unsigned_short_int_max=tipo_unsigned_short_int+(tipo_unsigned_short_int-1); tipo_unsigned_short_int=tipo_unsigned_short_int*2; //printf("admite como maximo: %i \n\n",tipo_unsigned_short_int); bits++; } printf("unsigned short int .. %ibits .. de %i a %i \n\n",bits,tipo_unsigned_short_int,tipo_unsigned_short_int_max); bits=1; while(tipo_int>0){ tipo_int_max=tipo_int+(tipo_int-1); tipo_int=tipo_int*2; //printf("admite como maximo: %i \n\n",tipo_long_int); bits++; } printf("int ................. %ibits .. de %i a %i \n\n",bits,tipo_int,tipo_int_max); bits=1; while(tipo_long>0){ tipo_long_max=tipo_long+(tipo_long-1); tipo_long=tipo_long*2; //printf("admite como maximo: %ld \n\n",tipo_long_int); bits++; } printf("long ................ %ibits .. de %ld a %ld \n\n",bits,tipo_long,tipo_long_max); bits=1; while(tipo_long_int>0){ tipo_long_int_max=tipo_long_int+(tipo_long_int-1); tipo_long_int=tipo_long_int*2; //printf("admite como maximo: %i \n\n",tipo_long_int); bits++; } printf("long int ............ %ibits .. de %i a %i \n\n",bits,tipo_long_int,tipo_long_int_max); bits=0; while(tipo_unsigned_long_int>0){ tipo_unsigned_long_int_max=tipo_unsigned_long_int+(tipo_unsigned_long_int-1); tipo_unsigned_long_int=tipo_unsigned_long_int*2; //printf("admite como maximo: %i \n\n",tipo_unsigned_long_int); bits++; } printf("unsigned long int ... %ibits .. de %u a %u \n\n",bits,tipo_unsigned_long_int,tipo_unsigned_long_int_max); bits=0; while(tipo_unsigned_int>0){ tipo_unsigned_int_max=tipo_unsigned_int+(tipo_unsigned_int-1); tipo_unsigned_int=tipo_unsigned_int*2; //printf("admite como maximo: %i \n\n",tipo_unsigned_int); bits++; } printf("unsigned int ........ %ibits .. de %u a %u \n\n",bits,tipo_unsigned_int,tipo_unsigned_int_max); bits=0; while(tipo_unsigned_long>0){ tipo_unsigned_long_max=tipo_unsigned_long+(tipo_unsigned_long-1); tipo_unsigned_long=tipo_unsigned_long*2; //printf("admite como maximo: %i \n\n",tipo_unsigned_long); bits++; } printf("unsigned long ....... %ibits .. de %u a %u \n\n",bits,tipo_unsigned_long,tipo_unsigned_long_max); bits=1; while(tipo_long_long>0){ tipo_long_long_max=tipo_long_long+(tipo_long_long-1); tipo_long_long=tipo_long_long*2; //printf("admite como maximo: %ll \n",tipo_long_long); bits++; } printf("long long ........... %ibits .. de %lld a %lld \n\n",bits,tipo_long_long,tipo_long_long_max); bits=0; while(tipo_unsigned_long_long>0){ tipo_unsigned_long_long_max=tipo_unsigned_long_long+(tipo_unsigned_long_long-1); tipo_unsigned_long_long=tipo_unsigned_long_long*2; //printf("admite como maximo: %llu \n",tipo_unsigned_long_long); bits++; } printf("unsigned long long .. %ibits .. de %llu a %llu \n\n",bits,tipo_unsigned_long_long,tipo_unsigned_long_long_max); system("pause"); /* %d--> for int %ld--> for long int %lld--> for long long int %llu--> for unsigned long long int */ }
Y el resultado con Notepad++ y MinGW es este: char ................. 8bits de -128 a 127
unsigned char ........ 8bits .. de 0 a 255
short int ........... 16bits .. de -32768 a 32767
unsigned short int .. 16bits .. de 0 a 65535
int ................. 32bits .. de -2147483648 a 2147483647
long ................ 32bits .. de -2147483648 a 2147483647
long int ............ 32bits .. de -2147483648 a 2147483647
unsigned long int ... 32bits .. de 0 a 4294967295
unsigned int ........ 32bits .. de 0 a 4294967295
unsigned long ....... 32bits .. de 0 a 4294967295
long long ........... 64bits .. de -9223372036854775808 a 9223372036854775807
unsigned long long .. 64bits .. de 0 a 18446744073709551615
Título: Re: Poniendo a prueba los tipos de variables en c
Publicado por: ivancea96 en 26 Septiembre 2016, 19:30 pm
La razón por la cual están esas variables es porque no está asegurado que esos tamaños sean así siempre. Lo único asegurado es que los tamaños siguen estas condiciones: char <= short <= int <= long <= long long
|