How does the range of a specific type of data affect in C language?


Most videos in C show a table with the type of data, its size and range.

In the case of INT the value is between -32768 a 32767 .

I would like to know what this affects, since I put a value like this 33000 and it shows normal, which represents that 32767 and in its default the ranges in general

asked by Victor Alvarado 31.03.2017 в 21:12

3 answers


It really happens that the rando of a int in some platforms is that: -32768 to 32767 (platforms of 16 bits). But on 32-bit platforms, the int is larger. Probably you are on a modern platform (32 or 64 bits) so your int can accommodate a much larger range.

With this code you can know the size of a int on your platform:

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

int main(int argc, char **argv)
  printf("Tamaño del int: %d\n", sizeof(int));
  printf("INT_MAX: %d\n", INT_MAX);
  return 0;
answered by 31.03.2017 / 21:15

Well, the integral types in C and in most of the languages have something called limits , you can find a header in your C compiler called limits.h where all the limits are in your platform current.

The subject with the limits, is the following:

A int is 32 bytes, correct? A long long is 64 bytes.

Taking this to the hexadecimal system you can have the following values:

FF FF FF FF <- Int
FF FF FF FF FF FF FF FF <- Long long.

So, you have from 00 00 00 00 00 00 00 00 to FF FF FF FF FF FF FF FF in values for a long integer (long long).

But! It happens that when it comes to making the subject of signs in integral types ...

When a number has a sign, you only have half of its actual range of values available, since certain standards are used to represent the sign, for example the following program:

int main(void) {
    int test = 0xFFFFFFF0;
    printf("%d - %u\n", test, (unsigned int)test);

It will give you as a result:

-16 - 4294967280

And if you try to invert the bytes:

int main(void) {
    int test = 0x0000000f;
    printf("%d - %u\n", test, (unsigned int)test);

You will get the following result:

15 - 15

With all this, the size of some types (in bytes), this data may vary depending on the platform:

- char      : 1   <- Este es de tipo integral.
- short     : 2
- int       : 4
- float     : 4
- long      : 8 (64 bits)
- long long : 8
- double    : 8

Here below a program that shows it.

int main(void) {
    printf("sizeof(char)      == %d\n"
           "sizeof(short)     == %d\n"
           "sizeof(int)       == %d\n"
           "sizeof(float)     == %d\n"
           "sizeof(long)      == %d\n"
           "sizeof(long long) == %d\n"
           "sizeof(double)    == %d\n",
           sizeof(char), sizeof(short), sizeof(int),
           sizeof(long), sizeof(float), sizeof(long long), sizeof(double)
answered by 31.03.2017 в 21:34

As an additional reference, to know the current limits of a given type you can use the library limits .h (another C ++ link ):

#include <limits.h>

int main()
  printf("Rango para int (%d,%d)\n",INT_MIN,INT_MAX);
  printf("Rango para unsigned int (%u,%u)\n",UINT_MIN,UINT_MAX);
answered by 02.04.2017 в 02:32