(limits.h) in C/C++


The header files “limits.h” exists in C language while <climits> in C++ language. Several macros are defined in these header files. The limits specify that variable cannot store values beyond the limits.

Some macros in “limits.h” or <climits> header file are as follows

CHAR_BITLONG_MINLONG_MAXCHAR_MINCHAR_MAX
INT_MININT_MAXSHRT_MINSHRT_MAXULONG_MAX

Here is an example of <climits> in C++ language,

Example

 Live Demo

#include<iostream>
#include <bits/stdc++.h>
#include<climits>
using namespace std;
int main() {
   int x = 28;
   int a = CHAR_BIT*sizeof(x);
   stack<bool> s;
   cout << "The number is : " << x << endl;
   for (int i=1; i<=a; i++) {
      s.push(x%2);
      x = x/2;
   }
   cout << "The number of bits in a byte : " << CHAR_BIT << endl;
   for (int i=1; i<=a; i++) {
      cout << s.top();
      s.pop();
      if (i % CHAR_BIT == 0)
      cout << " ";
   }
   cout << "\n\nThe minimum value of short int :" << SHRT_MIN;
   return 0;
}

Output

The number is : 28
The number of bits in a byte : 8
00000000 00000000 00000000 00011100

The minimum value of short int :-32768

In the above program, two macros of <climits> are represented. One is CHAR_BIT and another is SHRT_MIN. CHAR_BIT defines the number of bits in char and SHRT_MIN defines the minimum value for short int.

int x = 28;
int a = CHAR_BIT*sizeof(x);
….
cout << "\n\nThe minimum value of short int :" << SHRT_MIN;

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

848 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements