tags:

views:

1021

answers:

3

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

Thanks,

+6  A: 

You'll want to use limits.h which provides the following constants (as per the linked Wikipedia page):

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

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 (again, courtesy of Wikipedia):

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 Wikipedia 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.

Mark E
what is the min and max value of a float?
SuperString
FLT_MAX and FLT_MIN
Martin Beckett
`SIZE_MAX` (maximum size of a `size_t`) is another useful one.
caf
+4  A: 

The header file limits.h defines macros that expand to various limits and parameters of the standard integer types. Also have a look at this.

Prasoon Saurav
what's the min value of unsigned char?
SuperString
@Superstring, the minimum value of any *unsigned* type is 0.
Mark E
I want negative unsigned values! :-)
Alok
And I want a pony
Martin Beckett
+1  A: 

Look at the these pages on limits.h and float.h, which are included as part of the standard c library.

Nixuz