C Library – <limits.h> ”; Previous Next The C Library limits.h header file defines various properties of the different variable types. The macros defined in this header, limits 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 character can store up to a maximum value of 255. <limits.h> Library Macros The following values are implementation-specific and defined using the #define directive. However, these values may not be lower than the given ones. 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 -128 Defines the minimum value for type char and its value will be equal to SCHAR_MIN if char represents negative values, otherwise zero. CHAR_MAX +127 Defines the value for type char and its value will be equal to SCHAR_MAX if char represents negative values, otherwise UCHAR_MAX. MB_LEN_MAX 16 Defines the maximum number of bytes in a multi-byte character. SHRT_MIN -32768 Defines the minimum value for a short int. SHRT_MAX +32767 Defines the maximum value for a short int. USHRT_MAX 65535 Defines the maximum value for an unsigned short int. INT_MIN -2147483648 Defines the minimum value for an int. INT_MAX +2147483647 Defines the maximum value for an int. UINT_MAX 4294967295 Defines the maximum value for an unsigned int. LONG_MIN -9223372036854775808 Defines the minimum value for a long int. LONG_MAX +9223372036854775807 Defines the maximum value for a long int. ULONG_MAX 18446744073709551615 Defines the maximum value for an unsigned long int. Note that, the above macros help developer to write efficient code that works consistently across different platforms and compilers. Example 1 Following is the basic C Library program to display the range value of various macros under the limits.h header file. #include <stdio.h> #include <limits.h> int main() { printf(“The value of CHAR_BIT: %dn”, CHAR_BIT); printf(“The value of SCHAR_MIN: %dn”, SCHAR_MIN); printf(“The value of SCHAR_MAX: %dn”, SCHAR_MAX); printf(“The value of UCHAR_MAX: %un”, UCHAR_MAX); printf(“The value of CHAR_MIN: %dn”, CHAR_MIN); printf(“The value of CHAR_MAX: %dn”, CHAR_MAX); printf(“The value of MB_LEN_MAX: %dn”, MB_LEN_MAX); printf(“The value of SHRT_MIN: %dn”, SHRT_MIN); printf(“The value of SHRT_MAX: %dn”, SHRT_MAX); printf(“The value of USHRT_MAX: %un”, USHRT_MAX); printf(“The value of INT_MIN: %dn”, INT_MIN); printf(“The value of INT_MAX: %dn”, INT_MAX); printf(“The value of UINT_MAX: %un”, UINT_MAX); printf(“The value of LONG_MIN: %ldn”, LONG_MIN); printf(“The value of LONG_MAX: %ldn”, LONG_MAX); printf(“The value of ULONG_MAX: %lun”, ULONG_MAX); return 0; } Output On execution of above code, we get the following result − The value of CHAR_BIT: 8 The value of SCHAR_MIN: -128 The value of SCHAR_MAX: 127 The value of UCHAR_MAX: 255 The value of CHAR_MIN: -128 The value of CHAR_MAX: 127 The value of MB_LEN_MAX: 16 The value of SHRT_MIN: -32768 The value of SHRT_MAX: 32767 The value of USHRT_MAX: 65535 The value of INT_MIN: -2147483648 The value of INT_MAX: 2147483647 The value of UINT_MAX: 4294967295 The value of LONG_MIN: -9223372036854775808 The value of LONG_MAX: 9223372036854775807 The value of ULONG_MAX: 18446744073709551615 Example 2 In this example, we illustrate the program for finding the minimum and maximum values. #include <stdio.h> #include <limits.h> int main() { int input[] = {89, 5, 21, 10, 45, 31, 98}; int n = sizeof(input) / sizeof(input[0]); // Initialization of macros int min_value = INT_MAX; int max_value = INT_MIN; // loop iteration for min and max value for (int i = 0; i < n; ++i) { if (input[i] < min_value) { min_value = input[i]; } if (input[i] > max_value) { max_value = input[i]; } } // Display the result printf(“Minimum value: %dn”, min_value); printf(“Maximum value: %dn”, max_value); return 0; } Output On execution of above code, we get the following result − Minimum value: 5 Maximum value: 98 Example 3 Below the program initializes limit macros(CHAR_MIN/MAX) to generate the minimum and maximum length of character from the given string. #include <stdio.h> #include <limits.h> #include <string.h> int main() { char* char_input[] = {“RUSSIA”, “UK”, “INDIA”, “NEWYORK”, “INDONESIA”, “DUBAI”}; int num_strings = sizeof(char_input) / sizeof(char_input[0]); // Initialization of CHAR macros int min_length = CHAR_MAX; int max_length = CHAR_MIN; for (int i = 0; i < num_strings; ++i) { int current_length = strlen(char_input[i]); if (current_length < min_length) { min_length = current_length; } if (current_length > max_length) { max_length = current_length; } } printf(“Minimum length of characters: %dn”, min_length); printf(“Maximum length of characters: %dn”, max_length); return 0; } Output The above code produces the following result − Minimum length of characters: 2 Maximum length of characters: 9
Print Page
Previous
Next
Advertisements ”;