
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
<climits> (limits.h) in C/C++
Both <climits> and "limits.h" are header files in C and C++ that are used to define constants related to data type limits.
Header files are usually created with a .h extension and are used to declare functions, variables, and other entities without having a main() function. You can also create custom header files in C and C++.
The <climits> header file is specific to the C++ language, whereas "limits.h" belongs to the C language. Let's discuss them one by one with the help of a suitable example to understand them and their uses in a program in a better way:
The <climits> in C++
The <climits> is a header file in C++ that is part of the "C++ Standard Library". This header defines various macros to represent the limits of various fundamental integral types such as int, char, short, etc.
You can use the <climits> header file to avoid the hard-coding of values like 32767 or 2147483647. By using this header file, you can directly use its macros such as INT_MIN and INT_MAX instead of hard-coding.
The <climits> header files is similar to the "limits.h" header file in C.
The following pseudo code will help you of how to use the <climits> header file in a C++ program:
#include<iostream> //included climits header file #include<climits> int main(){ // code implementation }
Below is a list of commonly used macros provided by these header files in C and C++:
Integral Type | Description |
---|---|
INT_MIN, INT_MAX | Minimum and maximum values for int |
CHAR_MIN, CHAR_MAX | Minimum and maximum values for char |
LONG_MIN, LONG_MAX | Minimum and maximum values for long |
SHRT_MIN, SHRT_MAX | Minimum and maximum values for short |
Let's use the <climits> header file in a C++ program to access the above macros:
Example 1
The following C++ example uses the <climits> header file to access the constants (macros) by using their name such as INT_MIN and INT_MAX:
#include<iostream> //include the <climits> header #include<climits> using namespace std; int main(){ //accessing constants using their name cout<<"The minimum value of int data type: "<<INT_MIN<<endl; cout<<"The maximum value of int data type: "<<INT_MAX; return 0; }
Following is the output of the above program:
The minimum value of int data type: -2147483648 The maximum value of int data type: 2147483647
Example 2
In the following example, we include the <climits> header to access the macros by name (i.e., INT_MIN and INT_MAX). We then use these macros to find the smallest and largest elements in an array by comparing each element with these macros:
#include<iostream> //include the <climits> header #include<climits> using namespace std; int main(){ int arr[] = {5, 1, 3, 6, 4, 2}; //accessing macros using their name int min = INT_MAX; int max = INT_MIN; //size of an array int length = sizeof(arr) / sizeof(arr[0]); cout<<"The array elements are: "; for(int i = 0; i<length; i++){ cout<<arr[i]<<" "; } for(int i = 0; i<length; i++){ if(arr[i] < min){ min = arr[i]; } else if(arr[i] > max){ max = arr[i]; } } cout<<"\nThe smallest element in an array is: "<<min; cout<<"\nThe largest element in an array is: "<<max; }
The above program produces the following output:
The array elements are: 5 1 3 6 4 2 The smallest element in an array is: 1 The largest element in an array is: 6
The "limits.h" in C
The "limits.h" is also a header file in the C language, which is exactly similar to the <climits> header file in C++. Like C++, it also defines the constants that represents the limits of fundamental integral data types.
The purpose of this header file is to provide a easiest way to access the minimum and maximum values that various integer types such as: char, int, short, long, and long long can hold on a particular system.
The following is the pseudo code that helps you how to use the "limits.h" header file in a C program:
//included header #include <limits.h> #inlcude <stdio.h> int main(){ //code implementation }
Here, the ".h" extension specifies that it is a header file. You can include it top of the C program before the main() method:
Example 1
The following is a basic example using the "limits.h" header file in C to access the minimum and maximum limits of the various data types:
#include <stdio.h> // include for integer limits macros #include <limits.h> int main() { // Character type limits printf("CHAR_BIT: %d bits in a char \n", CHAR_BIT); printf("CHAR_MIN: %d (Minimum value for char) \n", CHAR_MIN); printf("CHAR_MAX: %d (Maximum value for char) \n", CHAR_MAX); // Short integer type limits printf("SHRT_MIN: %d (Minimum value for short int) \n", SHRT_MIN); printf("SHRT_MAX: %d (Maximum value for short int) \n", SHRT_MAX); // Integer type limits printf("INT_MIN: %d (Minimum value for int) \n", INT_MIN); printf("INT_MAX: %d (Maximum value for int) \n", INT_MAX); return 0; }
Below is the output of the above program:
CHAR_BIT: 8 bits in a char CHAR_MIN: -128 (Minimum value for char) CHAR_MAX: 127 (Maximum value for char) SHRT_MIN: -32768 (Minimum value for short int) SHRT_MAX: 32767 (Maximum value for short int) INT_MIN: -2147483648 (Minimum value for int) INT_MAX: 2147483647 (Maximum value for int)
Conclusion
The header files <limits.h> and <climits> provide a set of pre-processor macros (i.e., INT_MIN, INT_MAX, CHAR_MIN, CHAR_MAX) that define the characteristics and limits (min and max) of fundamental integral data types such as int, char, short, long, etc.