By Mohamad Wael

Last updated :

Posted :

limits.h header in c a tutorial

The limits.h header, was defined as part of the c89 standard. It contains the min and the max limits, that integer types can have on a certain machine. The standard defines the minimum absolute value for the integer types, an implementation can define larger values.

The limits.h header, also defines the number of bits in a char, using the CHAR_BIT macro, and the maximum number of bytes in multibyte characters, using the MB_LEN_MAX macro.

limits.h header in c integer limits , number of bits in a char , maximum number of bytes in a multibyte character
Table of contents Integer types, min and max limits, as defined by the standard The CHAR_BIT and MB_LEN_MAX macros

1- Integer types, min and max limits, as defined by the standard

The table below, shows the minimum absolute value, for the integer types, as defined by the c standard. An implementation can define larger values.

limits.h Integer Type Limit signed char SCHAR_MIN -127 SCHAR_MAX +127 unsigned char UCHAR_MAX +255 char CHAR_MIN SCHAR_MIN or 0 , if char cannot have negative values CHAR_MAX SCHAR_MAX or UCHAR_MAX , if char cannot have negative values short SHRT_MIN -32767 SHRT_MAX +32767 unsigned short USHRT_MAX +65535 int INT_MIN -32767 INT_MAX +32767 unsigned int UINT_MAX +65535 long LONG_MIN -2147483647 LONG_MAX +2147483647 unsigned long ULONG_MAX +4294967295 long long LLONG_MIN -9223372036854775807 LLONG_MAX +9223372036854775807 unsigned long long ULLONG_MAX +18446744073709551615 Integer Type Limit

2- The CHAR_BIT and MB_LEN_MAX macros

The limits header also define two macros, they are the:

An example of a limits.h header, on a 64 bit system is:

#ifndef _LIMITS
#define _LIMITS

#define CHAR_BITS 8
/* Define the number of bits in a char */

#define MB_LEN_MAX 4
/*  Define the max number of bytes that a
    multibyte character can have for any
    supported local .*/

#define SCHAR_MAX 127
#define SCHAR_MIN ( -127 - 1 )
/*  Define the min and max values for
    the signed char type .*/

#define UCHAR_MAX 255u
/*  Define the max value for
    the unsigned char type .*/

#define CHAR_MAX 127
#define CHAR_MIN ( -127 - 1 )
/*  Define the min and max values for
    the char type .*/

#define SHRT_MAX 32767
#define SHRT_MIN ( -32767 - 1 )
/*  Define the min and max values
    for the signed short type .*/

#define USHRT_MAX 65535u
/*  Define the max value for the
    unsigned short type .*/

#define INT_MAX 2147483647
#define INT_MIN ( -2147483647 - 1 )
/*  Define the min and max values for the
    int type .*/

#define UINT_MAX 4294967295U
/*  Define the max value for the unsigned
    int type .*/

#define LONG_MAX 9223372036854775807L
#define LONG_MIN (-9223372036854775807L - 1)
/*  Define the min and max values for the
    long type .*/

#define ULONG_MAX 18446744073709551615UL
/*  Define the max value for the unsigned
    long type .*/

#define LLONG_MAX 9223372036854775808LL
#define LLONG_MIN (-9223372036854775807LL - 1)
/*  Define the min and max values for the
    long long type .*/

#define ULLONG_MAX 18446744073709551615ULL
/*  Define the max value for the unsigned
    long long type .*/

#endif