elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Poniendo a prueba los tipos de variables en c
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: 1 [2] Ir Abajo Respuesta Imprimir
Autor Tema: Poniendo a prueba los tipos de variables en c  (Leído 5,833 veces)
ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Poniendo a prueba los tipos de variables en c
« Respuesta #10 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:
Código
  1. int8_t - uint8_t
  2. int16_t - uint16_t
  3. int32_t - uint32_t
  4. int64_t - uint64_t

Más información de los tipos en: 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)


En línea

Nucleorion

Desconectado Desconectado

Mensajes: 72


Ver Perfil
Re: Poniendo a prueba los tipos de variables en c
« Respuesta #11 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:
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. void main(){
  6. char tipo_char=1, tipo_char_max=1;                                                  //          -128 a 128
  7. unsigned char tipo_unsigned_char=1, tipo_unsigned_char_max=1;                       //             0 a 256
  8. short int tipo_short_int=1, tipo_short_int_max=1;                                   //        -32768 a 32767
  9. unsigned short int tipo_unsigned_short_int=1, tipo_unsigned_short_int_max=1;        //             0 a 65 535
  10. int tipo_int=1, tipo_int_max=1;                                                     //-2 147 483 648 a 2 147 483 647
  11. long tipo_long=1, tipo_long_max=1;                                                  //-2 147 483 648 a 2 147 483 647
  12. long int tipo_long_int=1, tipo_long_int_max=1;                                      //-2 147 483 648 a 2 147 483 647
  13. unsigned int tipo_unsigned_int=1, tipo_unsigned_int_max=1;                          //             0 a 4 294 967 295
  14. unsigned long tipo_unsigned_long=1, tipo_unsigned_long_max=1;                       //             0 a 4 294 967 295
  15. unsigned long int tipo_unsigned_long_int=1, tipo_unsigned_long_int_max=1;           //             0 a 4 294 967 295
  16. long long tipo_long_long=1, tipo_long_long_max=1;                            // -9223372036854775808 a  9223372036854775807
  17. unsigned long long tipo_unsigned_long_long=1, tipo_unsigned_long_long_max=1; //                    0 a 18 446 744 073 709 551 616
  18.  
  19. int bits=1;
  20.  
  21. while(tipo_char>0){
  22. tipo_char_max=tipo_char+(tipo_char-1);
  23. tipo_char=tipo_char*2;
  24. //printf("char admite como maximo: %i \n\n",tipo_char);
  25. bits++;
  26. }
  27. printf("char ................. %ibits de                    %i a                  %i \n\n",bits,tipo_char,tipo_char_max);
  28. bits=0;
  29. while(tipo_unsigned_char>0){
  30. tipo_unsigned_char_max=tipo_unsigned_char+(tipo_unsigned_char-1);
  31. tipo_unsigned_char=tipo_unsigned_char*2;
  32. bits++;
  33. }
  34. printf("unsigned char ........ %ibits .. de                    %i a                  %i \n\n",bits,tipo_unsigned_char,tipo_unsigned_char_max);
  35. bits=1;
  36. while(tipo_short_int>0){
  37. tipo_short_int_max=tipo_short_int+(tipo_short_int-1);
  38. tipo_short_int=tipo_short_int*2;
  39. bits++;
  40. }
  41. printf("short int ........... %ibits .. de               %i a                %i \n\n",bits,tipo_short_int,tipo_short_int_max);
  42. bits=0;
  43. while(tipo_unsigned_short_int>0){
  44. tipo_unsigned_short_int_max=tipo_unsigned_short_int+(tipo_unsigned_short_int-1);
  45. tipo_unsigned_short_int=tipo_unsigned_short_int*2;
  46. //printf("admite como maximo: %i \n\n",tipo_unsigned_short_int);
  47. bits++;
  48. }
  49. printf("unsigned short int .. %ibits .. de                    %i a                %i \n\n",bits,tipo_unsigned_short_int,tipo_unsigned_short_int_max);
  50. bits=1;
  51. while(tipo_int>0){
  52. tipo_int_max=tipo_int+(tipo_int-1);
  53. tipo_int=tipo_int*2;
  54. //printf("admite como maximo: %i \n\n",tipo_long_int);
  55. bits++;
  56. }
  57. printf("int ................. %ibits .. de          %i a           %i \n\n",bits,tipo_int,tipo_int_max);
  58. bits=1;
  59. while(tipo_long>0){
  60. tipo_long_max=tipo_long+(tipo_long-1);
  61. tipo_long=tipo_long*2;
  62. //printf("admite como maximo: %ld \n\n",tipo_long_int);
  63. bits++;
  64. }
  65. printf("long ................ %ibits .. de          %ld a           %ld \n\n",bits,tipo_long,tipo_long_max);
  66. bits=1;
  67. while(tipo_long_int>0){
  68. tipo_long_int_max=tipo_long_int+(tipo_long_int-1);
  69. tipo_long_int=tipo_long_int*2;
  70. //printf("admite como maximo: %i \n\n",tipo_long_int);
  71. bits++;
  72. }
  73. printf("long int ............ %ibits .. de          %i a           %i \n\n",bits,tipo_long_int,tipo_long_int_max);
  74. bits=0;
  75. while(tipo_unsigned_long_int>0){
  76. tipo_unsigned_long_int_max=tipo_unsigned_long_int+(tipo_unsigned_long_int-1);
  77. tipo_unsigned_long_int=tipo_unsigned_long_int*2;
  78. //printf("admite como maximo: %i \n\n",tipo_unsigned_long_int);
  79. bits++;
  80. }
  81. printf("unsigned long int ... %ibits .. de                    %u a           %u \n\n",bits,tipo_unsigned_long_int,tipo_unsigned_long_int_max);
  82. bits=0;
  83. while(tipo_unsigned_int>0){
  84. tipo_unsigned_int_max=tipo_unsigned_int+(tipo_unsigned_int-1);
  85. tipo_unsigned_int=tipo_unsigned_int*2;
  86. //printf("admite como maximo: %i \n\n",tipo_unsigned_int);
  87. bits++;
  88. }
  89. printf("unsigned int ........ %ibits .. de                    %u a           %u \n\n",bits,tipo_unsigned_int,tipo_unsigned_int_max);
  90. bits=0;
  91. while(tipo_unsigned_long>0){
  92. tipo_unsigned_long_max=tipo_unsigned_long+(tipo_unsigned_long-1);
  93. tipo_unsigned_long=tipo_unsigned_long*2;
  94. //printf("admite como maximo: %i \n\n",tipo_unsigned_long);
  95. bits++;
  96. }
  97. printf("unsigned long ....... %ibits .. de                    %u a           %u \n\n",bits,tipo_unsigned_long,tipo_unsigned_long_max);
  98. bits=1;
  99. while(tipo_long_long>0){
  100. tipo_long_long_max=tipo_long_long+(tipo_long_long-1);
  101. tipo_long_long=tipo_long_long*2;
  102. //printf("admite como maximo: %ll \n",tipo_long_long);
  103. bits++;
  104. }
  105. printf("long long ........... %ibits .. de %lld a  %lld \n\n",bits,tipo_long_long,tipo_long_long_max);
  106. bits=0;
  107. while(tipo_unsigned_long_long>0){
  108. tipo_unsigned_long_long_max=tipo_unsigned_long_long+(tipo_unsigned_long_long-1);
  109. tipo_unsigned_long_long=tipo_unsigned_long_long*2;
  110. //printf("admite como maximo: %llu \n",tipo_unsigned_long_long);
  111. bits++;
  112. }
  113. printf("unsigned long long .. %ibits .. de                    %llu a %llu \n\n",bits,tipo_unsigned_long_long,tipo_unsigned_long_long_max);
  114.  
  115. system("pause");
  116.  
  117.  
  118. /*
  119. %d--> for int
  120.  
  121. %ld--> for long int
  122.  
  123. %lld--> for long long int
  124.  
  125. %llu--> for unsigned long long int
  126. */
  127. }
  128.  

Y el resultado con Notepad++ y MinGW es este:

Citar
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


En línea

ivancea96


Desconectado Desconectado

Mensajes: 3.412


ASMático


Ver Perfil WWW
Re: Poniendo a prueba los tipos de variables en c
« Respuesta #12 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:
Código
  1. char <= short <= int <= long <= long long
En línea

Páginas: 1 [2] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines