Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C Standard Library – <limits.h>


May 11, 2021 C


Table of contents


C Standard Library - slt;limits.h>

Brief introduction

The limits.h header file determines the various properties of the various variable types. The macro defined in the header file limits the values of various variable types, such as char, int, and long.

These limits specify that variables cannot store any values that exceed these limits, for example, the maximum value that an unsigned can store is 255.

Library macros

The following values are specific and are defined by the #define directive, and they must not be lower than the values given below.

Macro value describe
CHAR_BIT 8 Define the number of bits of one byte.
SCHAR_MIN -128 Define a minimum of symbolic characters.
SCHAR_MAX 127 Define a maximum of symbolic characters.
UCHAR_MAX 255 Define a maximum of a symbolic characters.
CHAR_MIN 0 Define the minimum value of type char, if char is negative, then its value is equal to SCHAR_MIN, otherwise equal to 0.
CHAR_MAX 127 Define the maximum value of type char, if char is negative, it is equal to SCHAR_MAX, otherwise it is equal to UCHAR_MAX.
MB_LEN_MAX 1 Define the maximum number of bytes in multi-byte characters.
SHRT_MIN -32768 Define a short integer minimum.
SHRT_MAX +32767 Define a short integer maximum.
USHRT_MAX 65535 Define a maximum of unsigned short integer.
INT_MIN -32768 Define a minimum of a whole.
INT_MAX +32767 Define a maximum maximum.
UINT_MAX 65535 Define a maximum of unsigned integer.
LONG_MIN -2147483648 Define a long minimum.
LONG_MAX +2147483647 Define a maximum of a long integer.
ULONG_MAX 4294967295 Define a maximum of unsigned long integer.

The following example demonstrates the use of some of the 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 program above, which will produce the following results:

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 = -32768
The maximum value of INT = 32767
The minimum value of CHAR = -128
The maximum value of CHAR = 127
The minimum value of LONG = -2147483648
The maximum value of LONG = 2147483647