Monday, May 16, 2011

How can you determine the maximum value that a numeric variable can hold?

The easiest way to find out how large or small a number that a particular type can hold is to use the values
defined in the ANSI standard header file limits.h. This file contains many useful constants defining the values
that can be held by various types, including these:
Value Description
CHAR_BIT Number of bits in a char
CHAR_MAX Maximum decimal integer value of a char
CHAR_MIN Minimum decimal integer value of a char
MB_LEN_MAX Maximum number of bytes in a multibyte character
INT_MAX Maximum decimal value of an int
INT_MIN Minimum decimal value of an int
LONG_MAX Maximum decimal value of a long
LONG_MIN Minimum decimal value of a long
SCHAR_MAX Maximum decimal integer value of a signed char
SCHAR_MIN Minimum decimal integer value of a signed char
SHRT_MAX Maximum decimal value of a short
SHRT_MIN Minimum decimal value of a short
UCHAR_MAX Maximum decimal integer value of unsigned char
UINT_MAX Maximum decimal value of an unsigned integer
ULONG_MAX Maximum decimal value of an unsigned long int
USHRT_MAX Maximum decimal value of an unsigned short int
For integral types, on a machine that uses two’s complement arithmetic (which is just about any machine
you’re likely to use), a signed type can hold numbers from –2(number of bits – 1) to +2(number of bits – 1) – 1. An unsigned
type can hold values from 0 to +2(number of bits) – 1. For instance, a 16-bit signed integer can hold numbers from
–215 (–32768) to +215 – 1 (32767).