SuperString - 1 year ago 110

C Question

What is the function to determine the min and max possible of value of datatypes (i.e, int, char.etc) in C?

Answer Source

You'll want to use `limits.h`

which provides the following constants (as per the linked reference):

```
CHAR_BIT = number of bits in a char
SCHAR_MIN = minimum value for a signed char
SCHAR_MAX = maximum value for a signed char
UCHAR_MAX = maximum value for an unsigned char
CHAR_MIN = minimum value for a char
CHAR_MAX = maximum value for a char
MB_LEN_MAX = maximum multibyte length of a character accross locales
SHRT_MIN = minimum value for a short
SHRT_MAX = maximum value for a short
USHRT_MAX = maximum value for an unsigned short
INT_MIN = minimum value for an int
INT_MAX = maximum value for an int
UINT_MAX = maximum value for an unsigned int
LONG_MIN = minimum value for a long
LONG_MAX = maximum value for a long
ULONG_MAX = maximum value for an unsigned long
LLONG_MIN = minimum value for a long long
LLONG_MAX = maximum value for a long long
ULLONG_MAX = maximum value for an unsigned long long
```

Where `U*_MIN`

is omitted for obvious reasons (any unsigned type has a minimum value of 0).

Similarly `float.h`

provides limits for `float`

and `double`

types:

```
FLT_MIN = min value of a float
FLT_MAX = max value of a float
DBL_MIN = min value of a double
DBL_MAX = max value of a double
LDBL_MIN = min value of a long double
LDBL_MAX = max value of a long double
```

You should read the article on `floats.h`

carefully, though `float`

and `double`

can hold the prescribed minimum and maximum values but the precision with which each type can represent data may not match what it is you're trying to store. In particular, it's difficult to store exceptionally large numbers with extremely small fractions attached. So `float.h`

provides a number of other constants that help you to determine if a `float`

or a `double`

can,in fact,represent a particular number.