-8

I have been watched a video lecture on the youtube and in that video the person able to use the INT_MIN And INT_MAX without inserting the "algorithm" header file (i.e.#include < algorithm > ) in the source code file. but in my case when i try to use the INT_MIN or INT_MAX without "algorithm" header , compiler throw an error . but when i use the INT_MIN or INT_MAX with the "algorithm" header it does't throw any errror. why ?

just explanatoin how it is happening. :( ?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 10
    don't try to learn c++ from youtube videos. It will not work out. Try [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and here: https://en.cppreference.com/w/c/types/limits – 463035818_is_not_an_ai Aug 29 '23 at 10:20
  • 3
    Some system headers might include other headers but you should not rely on that. These constants are included in `` – Quimby Aug 29 '23 at 10:22
  • 1
    Some implementation might include in their headers extra headers (as `` from `` with gcc), don't rely on that. – Jarod42 Aug 29 '23 at 10:23
  • 8
    c++ has a confusing feature of transitive includes. What might work in the video does not have to work for you. The video should have included the right header, but they didnt. Note that every idiot can make a youtube video and call it a "tutorial". A good tutorial would have told you what header to include. – 463035818_is_not_an_ai Aug 29 '23 at 10:23
  • 1
    In C++, using `std::numeric_limits::max()` has some advantages. It's a bit more verbose, yes, but the names are simpler to remember for all the different types. For instance, there is no `SHORT_MAX` but there is `std::numeric_limits::max()` – MSalters Aug 29 '23 at 10:28
  • 1
    `INT_MIN` and `INT_MAX` are only guaranteed to be defined through `` (due to backward compatibility to C, although formally deprecated in C++) or `` (which provides that facility in C++). They are not guaranteed to be defined by other headers (although C++ standard headers are *not forbidden* from including `` and/or ``, they are *not required to* either). It is often considered preferable in C++ to `#include ` and use `std::numeric_limits::min()` and `std::numeric_limits::max()`. – Peter Aug 29 '23 at 10:48

2 Answers2

3

In C++ (like in C), almost every name is defined in some kind of header. INT_MAX is defined in <climits>. The video you saw is not so good; it worked only because that particular implementation had an <algorithm> header which itself included <climits>. This sort of details can break when you upgrade your compiler - if you need <climits>, include it.

Your C++ book should tell you what to include for every name. <algorithm> includes about a hundred different names, all from namespace std and namespace std::ranges.

MSalters
  • 173,980
  • 10
  • 155
  • 350
2

INT_MIN and INT_MAX are not part of the <algorithm> header. They are actually defined in the <climits> header (or <limits.h> in C). If you're not including this header but are still able to use INT_MIN and INT_MAX, it's likely that some other included header internally includes <climits>.

Your issue could be because of differences in compiler settings, or perhaps the lecturer's code has other includes that indirectly bring in INT_MIN and INT_MAX.

To use INT_MIN and INT_MAX explicitly, include the <climits> header.

#include <climits>

int main() {
  int min_val = INT_MIN;
  int max_val = INT_MAX;
}

That should resolve the compiler error. Note using, std::numeric_limits is more type-safe and flexible. It works with custom numeric types and is more in line with C++ idioms. It's part of the <limits> header and allows you to query properties for all built-in types and any user-defined types that specialize std::numeric_limits.

Example:

#include <limits>

int main() {
  int max_int = std::numeric_limits<int>::max();
  int min_int = std::numeric_limits<int>::min();
  short max_short = std::numeric_limits<short>::max();
}

Using std::numeric_limits is considered idiomatic in modern C++ and offers advantages in terms of type safety and code maintainability.

NoName
  • 643
  • 3
  • 8