use of climits in c++

What is use of #include<climits> in C++ ?

cout<<SHRT_MIN ;
This command gives output without climits. Then what is use of <climits> ?
Last edited on
It's possible your implementation of <iostream> (or another header) includes <limits.h> or <climits> somewhere.
This is not required by the standard though, and won't work for all compilers.

For example, try compiling this. Won't work.
https://ideone.com/O9z4p8

___________________________

In general, always #include the header that defines the function/type/macro you need, to ensure portability between different compilers. Worst case scenario is that it skips the header file the 2nd time it encounters it because of proper header guards, no damage done.
Last edited on
Ganado +1

There is also the difference between languages.

C
1
2
3
4
5
6
7
8
#include <limits.h>
#include <stdio.h>

int main()
{
  printf( "%d\n", SHRT_MIN );  /* A really dumb name, should have been SHORT_MIN, LOL */ 
  return 0;
}

C++
1
2
3
4
5
6
7
#include <iostream>
#include <limits>

int main()
{
  std::cout << std::numeric_limits <short> ::min() << "\n";
}

Enjoy!
Thanks. I am using a dev c++ compiler. In that climits is not required for these scenarios. Program is giving output even without using climits library.
@abcdef123 Try this in Dev C++.

1
2
3
4
5
6
int main()
{
    short a = SHRT_MIN;
    short b = std::numeric_limits <short> ::min();
    return a - b;
}

It needs both headers in order to compile:
1
2
#include <limits.h>
#include <limits> 

@abcdef123
It appears you didn't even read Ganado's answer. Let me put this as simply as possible:

1. You create homework that compiles on your machine
2. You submit homework to professor, who will be grading your efforts
3. Your homework does not compile on your professor's machine
4. You receive a 'D' for minimal effort

Here's another scenario:

1. You create application that compiles on your machine
2. You commit changes to work computer, so that you can get paid
3. Your application fails to compile on client's machine and causes late-night roll back meeting involving at least three engineers
4. You get fired

The takeaway: If you use a construct, include its header.
Topic archived. No new replies allowed.