• Home
  • 1.27 Basic C Type Limits

1.27 Basic C Type Limits

View Categories

1.27 Basic C Type Limits

1 min read

Table of Contents

C defines minimum ranges for its fundamental integer types. The exact size and range of a type can vary between implementations, so programs should not assume that a type always has a particular size.

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

int main()
{
    printf("CHAR_BIT  = %d\n", CHAR_BIT);
    printf("CHAR_MIN  = %d\n", CHAR_MIN);
    printf("CHAR_MAX  = %d\n", CHAR_MAX);

    printf("INT_MIN   = %d\n", INT_MIN);
    printf("INT_MAX   = %d\n", INT_MAX);

    printf("LONG_MIN  = %ld\n", LONG_MIN);
    printf("LONG_MAX  = %ld\n", LONG_MAX);

    return 0;
}

Example Output #

On a typical implementation:

CHAR_BIT  = 8
CHAR_MIN  = -128
CHAR_MAX  = 127
INT_MIN   = -2147483648
INT_MAX   = 2147483647
LONG_MIN  = -9223372036854775808
LONG_MAX  = 9223372036854775807

Explanation #

The <limits.h> header provides macros describing implementation-defined limits for integer types.

Macro Describes
CHAR_BIT Number of bits in a byte
CHAR_MIN Minimum value of char
CHAR_MAX Maximum value of char
SCHAR_MIN Minimum value of signed char
SCHAR_MAX Maximum value of signed char
UCHAR_MAX Maximum value of unsigned char
SHRT_MIN Minimum value of short
SHRT_MAX Maximum value of short
USHRT_MAX Maximum value of unsigned short
INT_MIN Minimum value of int
INT_MAX Maximum value of int
UINT_MAX Maximum value of unsigned int
LONG_MIN Minimum value of long
LONG_MAX Maximum value of long
ULONG_MAX Maximum value of unsigned long
LLONG_MIN Minimum value of long long
LLONG_MAX Maximum value of long long
ULLONG_MAX Maximum value of unsigned long long

For example:

printf("%d\n", INT_MAX);

prints the largest value that an int can represent on the implementation.

The actual limits should be obtained from <limits.h> rather than assumed from a particular compiler or processor.

Powered by BetterDocs

Leave a Reply

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