• Home
  • 1.28 Basic Numeric Limits

1.28 Basic Numeric Limits

View Categories

1.28 Basic Numeric Limits

1 min read

Table of Contents

C provides limits for integer and floating-point types through standard headers. Integer limits are available through <limits.h>, while floating-point limits are provided by <float.h>.

#include <stdio.h>
#include <limits.h>
#include <float.h>

int main()
{
    printf("INT_MAX     = %d\n", INT_MAX);
    printf("UINT_MAX    = %u\n", UINT_MAX);

    printf("FLT_MAX     = %e\n", FLT_MAX);
    printf("DBL_MAX     = %e\n", DBL_MAX);

    printf("FLT_MIN     = %e\n", FLT_MIN);
    printf("DBL_MIN     = %e\n", DBL_MIN);

    return 0;
}

Example Output #

A typical implementation may produce:

INT_MAX     = 2147483647
UINT_MAX    = 4294967295
FLT_MAX     = 3.402823e+38
DBL_MAX     = 1.797693e+308
FLT_MIN     = 1.175494e-38
DBL_MIN     = 2.225074e-308

Explanation #

<limits.h> provides limits for integer types:

INT_MAX
UINT_MAX

<float.h> provides characteristics and limits for floating-point types:

FLT_MAX
DBL_MAX
FLT_MIN
DBL_MIN

Here, FLT_MIN and DBL_MIN represent the smallest positive normalized values, not the most negative values.

Macro Meaning
FLT_MIN Smallest positive normalized float
FLT_MAX Largest finite float
DBL_MIN Smallest positive normalized double
DBL_MAX Largest finite double
LDBL_MIN Smallest positive normalized long double
LDBL_MAX Largest finite long double

Floating-point characteristics also include precision-related macros such as:

Macro Meaning
FLT_DIG Decimal digits of precision for float
DBL_DIG Decimal digits of precision for double
LDBL_DIG Decimal digits of precision for long double
FLT_EPSILON Difference between 1.0 and the next representable float
DBL_EPSILON Difference between 1.0 and the next representable double
LDBL_EPSILON Difference between 1.0L and the next representable long double

The exact values depend on the implementation. Using these macros is preferable to assuming a particular floating-point representation or range.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *