Numeric limits

From cppreference.com
< c‎ | types

Contents

[edit] Limits of library types

Defined in header <stdint.h>
PTRDIFF_MIN
(C99)
minimum value of object of ptrdiff_t type
(macro constant)
PTRDIFF_MAX
(C99)
maximum value of object of ptrdiff_t type
(macro constant)
SIZE_MAX
(C99)
maximum value of object of size_t type
(macro constant)
SIG_ATOMIC_MIN
(C99)
minimum value of object of sig_atomic_t type
(macro constant)
SIG_ATOMIC_MAX
(C99)
maximum value of object of sig_atomic_t type
(macro constant)
Defined in header <wchar.h>
WCHAR_MIN
(C99)
minimum value of object of wchar_t type
(macro constant)
WCHAR_MAX
(C99)
maximum value of object of wchar_t type
(macro constant)
WINT_MIN
(C99)
minimum value of object of wint_t type
(macro constant)
WINT_MAX
(C99)
maximum value of object of wint_t type
(macro constant)

[edit] Example

#include <stdio.h>
#include <stdint.h>
#include <wchar.h>
 
int main(void)
{
    printf("PTRDIFF_MIN    = %td\n", PTRDIFF_MIN);
    printf("PTRDIFF_MAX    = %+td\n", PTRDIFF_MAX);
    printf("SIZE_MAX       = %zu\n", SIZE_MAX);
    printf("SIG_ATOMIC_MIN = %+jd\n",(intmax_t)SIG_ATOMIC_MIN);
    printf("SIG_ATOMIC_MAX = %+jd\n",(intmax_t)SIG_ATOMIC_MAX);
    printf("WCHAR_MIN      = %+jd\n",(intmax_t)WCHAR_MIN);
    printf("WCHAR_MAX      = %+jd\n",(intmax_t)WCHAR_MAX);
    printf("WINT_MIN       = %jd\n", (intmax_t)WINT_MIN);
    printf("WINT_MAX       = %jd\n", (intmax_t)WINT_MAX);
}

Possible output:

PTRDIFF_MIN    = -9223372036854775808
PTRDIFF_MAX    = +9223372036854775807
SIZE_MAX       = 18446744073709551615
SIG_ATOMIC_MIN = -2147483648
SIG_ATOMIC_MAX = +2147483647
WCHAR_MIN      = -2147483648
WCHAR_MAX      = +2147483647
WINT_MIN       = 0
WINT_MAX       = 4294967295

[edit] Limits of integer types

Defined in header <limits.h>
CHAR_BIT
number of bits in byte
(macro constant)
MB_LEN_MAX
maximum number of bytes in a multibyte character
(macro constant)
CHAR_MIN
minimum value of char
(macro constant)
CHAR_MAX
maximum value of char
(macro constant)
SCHAR_MIN
SHRT_MIN
INT_MIN
LONG_MIN
LLONG_MIN
(C99)
minimum value of signed char, short, int, long and long long respectively
(macro constant)
SCHAR_MAX
SHRT_MAX
INT_MAX
LONG_MAX
LLONG_MAX
(C99)
maximum value of signed char, short, int, long and long long respectively
(macro constant)
UCHAR_MAX
USHRT_MAX
UINT_MAX
ULONG_MAX
ULLONG_MAX
(C99)
maximum value of unsigned char, unsigned short, unsigned int,
unsigned long and unsigned long long respectively
(macro constant)

[edit] Example

#include <stdio.h>
#include <limits.h>
 
int main(void)
{   
    printf("CHAR_BIT   = %d\n", CHAR_BIT);
    printf("MB_LEN_MAX = %d\n", MB_LEN_MAX);
    printf("\n");
 
    printf("CHAR_MIN   = %+d\n", CHAR_MIN);
    printf("CHAR_MAX   = %+d\n", CHAR_MAX);
    printf("SCHAR_MIN  = %+d\n", SCHAR_MIN);
    printf("SCHAR_MAX  = %+d\n", SCHAR_MAX);
    printf("UCHAR_MAX  = %u\n",  UCHAR_MAX);
    printf("\n");
 
    printf("SHRT_MIN   = %+d\n", SHRT_MIN);
    printf("SHRT_MAX   = %+d\n", SHRT_MAX);
    printf("USHRT_MAX  = %u\n",  USHRT_MAX);
    printf("\n");
 
    printf("INT_MIN    = %+d\n", INT_MIN);
    printf("INT_MAX    = %+d\n", INT_MAX);
    printf("UINT_MAX   = %u\n",  UINT_MAX);
    printf("\n");
 
    printf("LONG_MIN   = %+ld\n", LONG_MIN);
    printf("LONG_MAX   = %+ld\n", LONG_MAX);
    printf("ULONG_MAX  = %lu\n",  ULONG_MAX);
    printf("\n");
 
    printf("LLONG_MIN  = %+lld\n", LLONG_MIN);
    printf("LLONG_MAX  = %+lld\n", LLONG_MAX);
    printf("ULLONG_MAX = %llu\n",  ULLONG_MAX);
    printf("\n");
}

Possible output:

CHAR_BIT   = 8
MB_LEN_MAX = 16
 
CHAR_MIN   = -128
CHAR_MAX   = +127
SCHAR_MIN  = -128
SCHAR_MAX  = +127
UCHAR_MAX  = 255
 
SHRT_MIN   = -32768
SHRT_MAX   = +32767
USHRT_MAX  = 65535
 
INT_MIN    = -2147483648
INT_MAX    = +2147483647
UINT_MAX   = 4294967295
 
LONG_MIN   = -9223372036854775808
LONG_MAX   = +9223372036854775807
ULONG_MAX  = 18446744073709551615
 
LLONG_MIN  = -9223372036854775808
LLONG_MAX  = +9223372036854775807
ULLONG_MAX = 18446744073709551615

[edit] Limits of floating point types

Defined in header <float.h>
FLT_RADIX
the radix (integer base) used by the representation of all three floating-point types
(macro constant)
DECIMAL_DIG
(C99)
number of decimal digits that can be converted to long double and back without losing precision
(macro constant)
FLT_MIN
DBL_MIN
LDBL_MIN
minimum, normalized, positive value of float, double and long double respectively
(macro constant)
FLT_MAX
DBL_MAX
LDBL_MAX
maximum value of float, double and long double respectively
(macro constant)
FLT_EPSILON
DBL_EPSILON
LDBL_EPSILON
difference between 1.0 and the next representable value for float, double and long double respectively
(macro constant)
FLT_DIG
DBL_DIG
LDBL_DIG
number of decimal digits that can be represented without losing precision for float, double and long double respectively
(macro constant)
FLT_MANT_DIG
DBL_MANT_DIG
LDBL_MANT_DIG
number of base-FLT_RADIX digits that are in the floating-point mantissa and that can be represented without losing precision for float, double and long double respectively
(macro constant)
FLT_MIN_EXP
DBL_MIN_EXP
LDBL_MIN_EXP
minimum negative integer such that FLT_RADIX raised by power one less than that integer is a normalized float, double and long double respectively
(macro constant)
FLT_MIN_10_EXP
DBL_MIN_10_EXP
LDBL_MIN_10_EXP
minimum negative integer such that 10 raised by power one less than that integer is a normalized float, double and long double respectively
(macro constant)
FLT_MAX_EXP
DBL_MAX_EXP
LDBL_MAX_EXP
maximum positive integer such that FLT_RADIX raised by power one less than that integer is a normalized float, double and long double respectively
(macro constant)
FLT_MAX_10_EXP
DBL_MAX_10_EXP
LDBL_MAX_10_EXP
maximum positive integer such that 10 raised by power one less than that integer is a normalized float, double and long double respectively
(macro constant)
rounding mode of floating-point arithmetics, equal to float_round_style
(macro constant)
use of extended precision for intermediate results: 0 not used, 1 double is used instead of float, 2: long double is used
(macro constant)
FLT_HAS_SUBNORM
DBL_HAS_SUBNORM
LDBL_HAS_SUBNORM
(C11)
whether the type supports subnormal (denormal) numbers: -1 indeterminable, 0 absent, 1 present
(macro constant)

[edit] Example

#include <stdio.h>
#include <float.h> 
#include <math.h>
 
int main(void)
{
    printf("FLT_RADIX    = %d\n", FLT_RADIX);
    printf("DECIMAL_DIG  = %d\n", DECIMAL_DIG);
    printf("FLT_MIN      = %e\n", FLT_MIN);
    printf("FLT_MAX      = %e\n", FLT_MAX);
    printf("FLT_EPSILON  = %e\n", FLT_EPSILON);
    printf("FLT_DIG      = %d\n", FLT_DIG);
    printf("FLT_MANT_DIG = %d\n", FLT_MANT_DIG);
    printf("FLT_MIN_EXP  = %d\n",  FLT_MIN_EXP);
    printf("FLT_MIN_10_EXP  = %d\n",  FLT_MIN_10_EXP);
    printf("FLT_MAX_EXP     = %d\n",  FLT_MAX_EXP);
    printf("FLT_MAX_10_EXP  = %d\n",  FLT_MAX_10_EXP);
    printf("FLT_ROUNDS      = %d\n",  FLT_ROUNDS);
    printf("FLT_EVAL_METHOD = %d\n",  FLT_EVAL_METHOD);
    printf("FLT_HAS_SUBNORM = %d\n",  FLT_HAS_SUBNORM);
}

Possible output:

FLT_RADIX    = 2
DECIMAL_DIG  = 37
FLT_MIN      = 1.175494e-38
FLT_MAX      = 3.402823e+38
FLT_EPSILON  = 1.192093e-07
FLT_DIG      = 6
FLT_MANT_DIG = 24
FLT_MIN_EXP  = -125
FLT_MIN_10_EXP  = -37
FLT_MAX_EXP     = 128
FLT_MAX_10_EXP  = 38
FLT_ROUNDS      = 1
FLT_EVAL_METHOD = 1
FLT_HAS_SUBNORM = 1