limits.h
#include
/* limits.h - limits of various variable types */
#ifndef _LIMITS_H_
#define _LIMITS_H_
/* CHARACTERS */
#define CHAR_BIT 8 // number of bits in a char
#define CHAR_MAX 127 // maximum value for a char
#define CHAR_MIN -128 // minimum value for a char
/* SIGNED INTEGERS */
#define SCHAR_MAX 127 // maximum value for a signed char
#define SCHAR_MIN -128 // minimum value for a signed char
#define SHRT_MAX 32767 // maximum value for a short int
#define SHRT_MIN -32768 // minimum value for a short int
#define INT_MAX 2147483647 // maximum value for an int
#define INT_MIN -2147483648 // minimum value for an int
#define LONG_MAX 9223372036854775807 // maximum value for a long int
#define LONG_MIN -9223372036854775808 // minimum value for a long int
#define LLONG_MAX 9223372036854775807 // maximum value for a long long int
#define LLONG_MIN -9223372036854775808 // minimum value for a long long int
/* UNSIGNED INTEGERS */
#define UCHAR_MAX 255 // maximum value for an unsigned char
#define USHRT_MAX 65535 // maximum value for an unsigned short int
#define UINT_MAX 4294967295 // maximum value for an unsigned int
#define ULONG_MAX 18446744073709551615 // maximum value for an unsigned long int
#define ULLONG_MAX 18446744073709551615 // maximum value for an unsigned long long int
/* OTHER */
#define MB_LEN_MAX 16 // maximum number of bytes in a multi-byte character
#endif
In this code snippet
we have defined the limits of various variable types using the limits.h header file. The code includes definitions for the maximum and minimum values of characters
signed integers
and unsigned integers. Each variable type has its corresponding limits defined
such as the maximum value for a char
short int
int
long int
and long long int.
The code also includes the number of bits in a char
which is 8 bits
and the maximum number of bytes in a multi-byte character
which is 16 bytes. These values are important for understanding the memory limitations of different variable types and how they can be used in programming.
By defining these limits in the limits.h header file
programmers can refer to these values when working with different variable types and ensure that their code remains within the bounds set by the language specifications. This helps to prevent overflow or underflow errors that can occur when working with variables that exceed their defined limits.