Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min and max value of data type in C

Tags:

c

types

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

like image 648
SuperString Avatar asked Jan 13 '10 01:01

SuperString


People also ask

What are the min and max integer values in C?

Value of INT_MAX is +2147483647. Value of INT_MIN is -2147483648.

Is there MIN and MAX in C?

However, both max() and min() are already part of the C++ standard library, in the <algorithm> header ( #include <algorithm> ).

Can max and min be used for any data type?

MAX and MIN operate on columns that contain character, graphic, numeric, date/time, and binary data (except for binary large object, or BLOB, data). The parentheses are required.


2 Answers

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

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 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 PTRDIFF_MIN    : minimum value of ptrdiff_t PTRDIFF_MAX    : maximum value of ptrdiff_t SIZE_MAX       : maximum value of size_t SIG_ATOMIC_MIN : minimum value of sig_atomic_t SIG_ATOMIC_MAX : maximum value of sig_atomic_t WINT_MIN       : minimum value of wint_t WINT_MAX       : maximum value of wint_t WCHAR_MIN      : minimum value of wchar_t WCHAR_MAX      : maximum value of wchar_t CHAR_BIT       : number of bits in a char MB_LEN_MAX     : maximum length of a multibyte character in bytes 

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    : smallest normalised positive value of a float FLT_MAX    : largest positive finite value of a float DBL_MIN    : smallest normalised positive value of a double DBL_MAX    : largest positive finite value of a double LDBL_MIN   : smallest normalised positive value of a long double LDBL_MAX   : largest positive finite value of a long double FLT_DIG    : the number of decimal digits guaranteed to be preserved converting from text to float and back to text DBL_DIG    : the number of decimal digits guaranteed to be preserved converting from text to double and back to text LDBL_DIG   : the number of decimal digits guaranteed to be preserved converting from text to long double and back to text 

Floating point types are symmetrical around zero, so the most negative finite number is the negation of the most positive finite number - eg float ranges from -FLT_MAX to FLT_MAX.

Do note that floating point types can only exactly represent a small, finite number of values within their range. As the absolute values stored get larger, the spacing between adjacent numbers that can be exactly represented also gets larger.

like image 168
Mark Elliot Avatar answered Sep 19 '22 01:09

Mark Elliot


"But glyph", I hear you asking, "what if I have to determine the maximum value for an opaque type whose maximum might eventually change?" You might continue: "What if it's a typedef in a library I don't control?"

I'm glad you asked, because I just spent a couple of hours cooking up a solution (which I then had to throw away, because it didn't solve my actual problem).

You can use this handy maxof macro to determine the size of any valid integer type.

#define issigned(t) (((t)(-1)) < ((t) 0))  #define umaxof(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | \                     (0xFULL << ((sizeof(t) * 8ULL) - 4ULL)))  #define smaxof(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | \                     (0x7ULL << ((sizeof(t) * 8ULL) - 4ULL)))  #define maxof(t) ((unsigned long long) (issigned(t) ? smaxof(t) : umaxof(t))) 

You can use it like so:

int main(int argc, char** argv) {     printf("schar: %llx uchar: %llx\n", maxof(char), maxof(unsigned char));     printf("sshort: %llx ushort: %llx\n", maxof(short), maxof(unsigned short));     printf("sint: %llx uint: %llx\n", maxof(int), maxof(unsigned int));     printf("slong: %llx ulong: %llx\n", maxof(long), maxof(unsigned long));     printf("slong long: %llx ulong long: %llx\n",            maxof(long long), maxof(unsigned long long));     return 0; } 

If you'd like, you can toss a '(t)' onto the front of those macros so they give you a result of the type that you're asking about, and you don't have to do casting to avoid warnings.

like image 29
Glyph Avatar answered Sep 22 '22 01:09

Glyph