What is the real lower limit for a (signed) long?

Checking long long type limits using

std::cout << std::numeric_limits<long long>::min(); 

I get -9223372036854775808

However, when compiling the following code:

 int main() { long long l; l=-9223372036854775808LL; } 

I get warnings:

 test.cpp:3:7: warning: integer constant is so large that it is unsigned. test.cpp:3: warning: this decimal constant is unsigned only in ISO C90 

What am I missing? Thanks so much in advance for your help.

Giorgio

+6
source share
3 answers

This 9223372036854775808LL is a positive number. Therefore you need to take

 std::cout << std::numeric_limits<long long>::max(); 

. Negation does not immediately lead to the fact that the operand of the operator - negative.

+13
source

It works great with std :: numeric and boost :: numeric; he does not give any warnings.

 #include <iostream> #include <boost/numeric/conversion/bounds.hpp> #include <boost/limits.hpp> int main(int argc, char* argv[]) { std::cout << "The minimum value for long long:\n"; std::cout << boost::numeric::bounds<long long>::lowest() << std::endl; std::cout << std::numeric_limits<long long>::min() << std::endl << std::endl; std::cout << "The maximum value for long long:\n"; std::cout << boost::numeric::bounds<long long>::highest() << std::endl; std::cout << std::numeric_limits<long long>::max() << std::endl << std::endl; std::cout << "The smallest positive value for long long:\n"; std::cout << boost::numeric::bounds<long long>::smallest() << std::endl << std::endl; long long l; l = boost::numeric::bounds<long long>::lowest(); std::cout << l << std::endl; l = std::numeric_limits<long long>::min(); std::cout << l << std::endl; return 0; } 
+1
source

In my system.

  {LLONG_MIN} Minimum value of type long long. Maximum Acceptable Value: -9223372036854775807 {LLONG_MAX} Maximum value of type long long. Minimum Acceptable Value: +9223372036854775807 

see limits.h file

It can also be found:

min : - 2^(sizeof (long long) - 1) - 1

max : + 2^(sizeof (long long) - 1)

0
source

Source: https://habr.com/ru/post/888701/


All Articles