English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
limits.h The header file determines various properties of various variable types. The macros defined in this header file limit the values of various variable types (such as char, int, and long).
These limitations specify that variables cannot store any values beyond these limitations, for example, the maximum value that an unsigned can store is 255.
The following values are specific to the implementation and are defined by the #define directive. These values must not be less than the values given below.
Macro | Value | Description |
---|---|---|
CHAR_BIT | 8 | Define the number of bits in a byte. |
SCHAR_MIN | -128 | Define the minimum value of a signed character. |
SCHAR_MAX | 127 | Define the maximum value of a signed character. |
UCHAR_MAX | 255 | Define the maximum value of an unsigned character. |
CHAR_MIN | 0 | Define the minimum value of the type char, if char represents a negative value, its value is equal to SCHAR_MIN, otherwise it is equal to 0. |
CHAR_MAX | 127 | Define o valor máximo do tipo char, se o char representa um valor negativo, seu valor é igual a SCHAR_MAX, caso contrário, é igual a UCHAR_MAX. |
MB_LEN_MAX | 1 | Define o número máximo de bytes de um caractere multibyte. |
SHRT_MIN | -32768 | Define o valor mínimo de um tipo short int. |
SHRT_MAX | +32767 | Define o valor máximo de um tipo short int. |
USHRT_MAX | 65535 | Define o valor máximo de um tipo unsigned short int. |
INT_MIN | -32768 | Define o valor mínimo de um tipo int. |
INT_MAX | +32767 | Define o valor máximo de um tipo int. |
UINT_MAX | 65535 | Define o valor máximo de um tipo unsigned int. |
LONG_MIN | -2147483648 | Define o valor mínimo de um tipo long int. |
LONG_MAX | +2147483647 | Define o valor máximo de um tipo long int. |
ULONG_MAX | 4294967295 | Define o valor máximo de um tipo unsigned long int. |
O exemplo a seguir demonstra o uso de algumas constantes definidas no arquivo limit.h.
#include <stdio.h> #include <limits.h> int main() { printf("O número de bits do tipo byte %d\n", CHAR_BIT); printf("O valor mínimo de SIGNED CHAR = %d\n", SCHAR_MIN); printf("O valor máximo de SIGNED CHAR = %d\n", SCHAR_MAX); printf("O valor máximo de UNSIGNED CHAR = %d\n", UCHAR_MAX); printf("O valor mínimo de SHORT INT = %d\n", SHRT_MIN); printf("O valor máximo de SHORT INT = %d\n", SHRT_MAX); printf("O valor mínimo de INT = %d\n", INT_MIN); printf("O valor máximo de INT = %d\n", INT_MAX); printf("O valor mínimo de CHAR = %d\n", CHAR_MIN); printf("O valor máximo de CHAR = %d\n", CHAR_MAX); printf("O valor máximo de LONG = %ld\n", LONG_MIN); printf("O valor mínimo de LONG = %ld\n", LONG_MAX); return(0); }
Vamos compilar e executar o programa acima, o que produzirá os seguintes resultados:
O número de bits do tipo byte 8 O valor mínimo de SIGNED CHAR = -128 O valor máximo de SIGNED CHAR = 127 Máximo de UNSIGNED CHAR = 255 Mínimo de SHORT INT = -32768 Máximo de SHORT INT = 32767 Mínimo de INT = -32768 Máximo de INT = 32767 Mínimo de CHAR = -128 Máximo de CHAR = 127 Máximo de LONG = -2147483648 Mínimo de LONG = 2147483647