Easy Tutorial
❮ C Preprocessors C Examples Prime Number ❯

C Standard Library - <limits.h>

Introduction

The limits.h header file determines various properties of the various variable types. The macros defined in this header file limit the values of various variable types like char, int, and long.

These limits specify that a variable cannot store any value beyond these limits, for example, an unsigned char can store a maximum value of 255.

Library Macros

The following values are implementation-specific and are defined using the #define directive, with values not less than those shown below.

Macro Value Description
CHAR_BIT 8 Defines the number of bits in a byte.
SCHAR_MIN -128 Defines the minimum value for a signed char.
SCHAR_MAX 127 Defines the maximum value for a signed char.
UCHAR_MAX 255 Defines the maximum value for an unsigned char.
CHAR_MIN 0 Defines the minimum value for type char, which is equal to SCHAR_MIN if char represents negative values, otherwise 0.
CHAR_MAX 127 Defines the maximum value for type char, which is equal to SCHAR_MAX if char represents negative values, otherwise UCHAR_MAX.
MB_LEN_MAX 1 Defines the maximum number of bytes in a multibyte character.
SHRT_MIN -32768 Defines the minimum value for a short integer.
SHRT_MAX +32767 Defines the maximum value for a short integer.
USHRT_MAX 65535 Defines the maximum value for an unsigned short integer.
INT_MIN -2147483648 Defines the minimum value for an integer.
INT_MAX 2147483647 Defines the maximum value for an integer.
UINT_MAX 4294967295 Defines the maximum value for an unsigned integer.
LONG_MIN -9223372036854775808 Defines the minimum value for a long integer.
LONG_MAX 9223372036854775807 Defines the maximum value for a long integer.
ULONG_MAX 1.8446744e+19 Defines the maximum value for an unsigned long integer.

Example

The following example demonstrates the use of some constants defined in the limit.h file.

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

int main()
{
    printf("The number of bits in a byte %d\n", CHAR_BIT);

    printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN);
    printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX);
    printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX);

    printf("The minimum value of SHORT INT = %d\n", SHRT_MIN);
    printf("The maximum value of SHORT INT = %d\n", SHRT_MAX);

    printf("The minimum value of INT = %d\n", INT_MIN);
    printf("The maximum value of INT = %d\n", INT_MAX);

    printf("The minimum value of CHAR = %d\n", CHAR_MIN);
    printf("The maximum value of CHAR = %d\n", CHAR_MAX);

    printf("The minimum value of LONG = %ld\n", LONG_MIN);
    printf("The maximum value of LONG = %ld\n", LONG_MAX);

    return 0;
}

Let's compile and run the above program, which will produce the following result:

The number of bits in a byte 8
The minimum value of SIGNED CHAR = -128
The maximum value of SIGNED CHAR = 127
The maximum value of UNSIGNED CHAR = 255
The minimum value of SHORT INT = -32768
The maximum value of SHORT INT = 32767
The minimum value of INT = -2147483648
The maximum value of INT = 2147483647
The minimum value of CHAR = -128
The maximum value of CHAR = 127
The minimum value of LONG = -9223372036854775808
The maximum value of LONG = 9223372036854775807
❮ C Preprocessors C Examples Prime Number ❯