Unfortunately memory allocated for any standard tipe is limited, so ranges of all all types have quite definite borders. Violated borders lead to computing errors.
To know borders (like min and max possible values) for integer types (not only int
, but also char
, short
, etc.) you can use limits.h
. E.g.:
#include <stdio.h>
#include <limits.h>
int main(void)
{
printf("INT_MIN = %d\n", INT_MIN);
printf("INT_MAX = %d\n", INT_MAX);
printf("LONG_MIN = %ld\n", LONG_MIN);
printf("LONG_MAX = %ld\n", LONG_MAX);
printf("LLONG_MIN = %lld\n", LLONG_MIN);
printf("LLONG_MAX = %lld\n", LLONG_MAX);
return 0;
}
Note:
1) values of limits depends on compiler and platform (e.g. for my system LONG_MIN
is equal to INT_MIN
, but perhaps, you will see different values);
2) limits.h
is just text file that can be oppend and explored
3) also learn about stdint.h
, that provide types with fixed sizes (and allows programs to be portable without "perhaps" in my first note)
4) there are some techniques to detect integer overflow