C limits library














































C limits library



C limits is a library used to get the maximum and minimum values of different datatypes and these datatypes do include derived datatypes such as arrays, pointers...  with the help of macro constants. Macro constants are used to find the sizes of the integral types like a character, short, integer, long integer, long long integer. These macro constants are used to find the minimum and maximum size of any integral type of data type.

These macros are defined in header file <climits> for C++ .The c limits library is generally used to simply find out the upper and the lower limit of the datatype that we have defined it makes programmers to easily get the max value of a datatype instead of remembering those values.
NOTE: These values may or may not be changed from one machine to other machine as it depends upon the configuration of the machine .
Let us try to understand the working of  <climits>   library with some examples.
// C++ program to demonstrate working of constants in climits.
Program 1:To find limits of different datatypes
#include <climits>
#include <iostream>

using namespace std;

int main()
{
cout << "CHAR_MIN : " << CHAR_MIN << endl;
cout << "CHAR_MAX : " << CHAR_MAX << endl;
cout << "SHRT_MIN : " << SHRT_MIN << endl;
cout << "SHRT_MAX : " << SHRT_MAX << endl;
cout << "USHRT_MAX : " << USHRT_MAX << endl;
cout << "INT_MIN : " << INT_MIN << endl;
cout << "INT_MAX : " << INT_MAX << endl;
cout << "UINT_MAX : " << UINT_MAX << endl;
cout << "LONG_MIN : " << LONG_MIN << endl;
cout << "LONG_MAX : " << LONG_MAX << endl;
cout << "ULONG_MAX : " << ULONG_MAX << endl;
cout << "LLONG_MIN : " << LLONG_MIN << endl;
cout << "LLONG_MAX : " << LLONG_MAX << endl;
cout << "ULLONG_MAX : " << ULLONG_MAX << endl;
return 0;
}

    here different macro constants are :

1. CHAR_MIN : 

Minimum value for an object of type char

Value of CHAR_MIN is either -127 (-27+1) or less

2. CHAR_MAX :  

Maximum value for an object of type char

Value of CHAR_MAX is either 127 (27-1) or 255 (28-1) or greater   

3. SHRT_MIN :  

Minimum value for an object of type short int

Value of SHRT_MIN is -32767 (-215+1) or less

4. SHRT_MAX :  

Maximum value for an object of type short int

Value of SHRT_MAX is 32767 (215-1) or greater

5. USHRT_MAX :  

Maximum value for an object of type unsigned short int   

Value of USHRT_MAX is 65535 (216-1) or greater

6. INT_MIN :  

Minimum value for an object of type int   

Value of INT_MIN is -32767 (-215+1) or less

7. INT_MAX : 

Maximum value for an object of type int   

Value of INT_MAX is 32767 (215-1) or greater

8. UINT_MAX :  

Maximum value for an object of type unsigned int   

Value of UINT_MAX is 65535 (216-1) or greater

9. LONG_MIN :  

Minimum value for an object of type long int   

Value of LONG_MIN is -2147483647 (-231+1) or less

10. LONG_MAX :  

Maximum value for an object of type long int   

Value of LONG_MAX is 2147483647 (231-1) or greater

11. ULONG_MAX :  

Maximum value for an object of type unsigned long int   

Value of ULONG_MAX is 4294967295 (232-1) or greater

12. LLONG_MIN :  

Minimum value for an object of type long long int   

Value of LLONG_MIN is -9223372036854775807 (-263+1) or less

13. LLONG_MAX :  

Maximum value for an object of type long long int   

Value of LLONG_MAX is 9223372036854775807 (263-1) or greater

14. ULLONG_MAX :  

Maximum value for an object of type unsigned long long int   

Value of ULLONG_MAX is 18446744073709551615 (264-1) or greater

Program 2:To find the Maximum and Minimum Element in an array


#include <iostream>

#include <climits>

using namespace std;


int main()

{

    int arr[100];

    int maxNo=INT_MIN;

//maxN0 to find the maximum element and intializing it  to INT_MIN

   //here we are intializing it with INT_MIN becaues as soon as any number is greater than the minimum number the maximum number gets updated

   //viceversa for minNo

   int minNo=INT_MAX;

   int n,i;

   cout<<"enter number of elements u want to enter in the array";

   cin>>n;// no of elements in the array user wants to enter

   //reading elements into the array

   cout<<"enter the elements in the array";

   for(i=0;i<n;i++){

       cin>>arr[i];

   }

   for(i=0;i<n;i++){

       maxNo=max(maxNo,arr[i]);//using built in function max to find the max element

       minNo=min(minNo,arr[i]);//using built in function min to find the min element

   }

   cout<<"maxNo is:"<<maxNo<<" "<<"minNo is:"<<minNo<<endl;

   

    return 0;

}












More Articles of Shaik Aftab Ahmed:

Name Views Likes
C++ Program to Find the Frequency of Odd & Even Numbers in the given Matrix 239 1
C++ program to Sort a Linked List Using Merge Sort 225 1
C++ Program to Implement a Linked List representation of a Binary tree 221 1
C++ Program to Check for balanced parentheses in an expression 146 1
C++ Program to Perform Inorder, Preorder, Postorder traversals in a binary tree. 181 1
C++ program to print Indian flag 201 1
C++ program to Convert a multi level linked list to a singly linked list 151 1
C++ program to print right view of a Binary tree using queue 163 1
C++ Program to implement Huffman Coding Compression Algorithm 225 1
C++ Program to Create a Height Balanced Binary Tree 160 1
C++ program to implement Prims algorithm 235 1
C++ Program for BFS Traversal 173 1
C++ Progam to Evaluate a Prefix Expression 167 1
C++ Program to Implement Queue using Linked List 175 1
C++ implementation of Stack using Linked list 173 1
C++ program to find the intersection point of two linked lists 228 1
C++ program to count the inversions in the given array 215 1
C++ program to perform D.N.F sort 227 1
C++ program to print all possible subsets of a String 215 1
C++ program to count the number of ones in a binary representation of a number 229 1
C++ program to print all possible subsets of a set 246 1
C++ program to find the largest word in a String 220 1
C++ Program to print a matrix in Spiral order 228 1
C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal 227 1
C limits library 255 1
Program to add two Binary numbers 231 1

Comments