11

I'm trying to write a test case for some corner case. For input of type int64_t, the following line won't compile:

int64_t a = -9223372036854775808LL;

The error/warning is:

error: integer constant is so large that it is unsigned [-Werror]

I thought the number was out of range, so I tried:

std::cout << std::numeric_limits<int64_t>::min() << std::endl;

It outputs exactly the same number!!! So the constant is within the range.

How can I fix this error?

Boann
  • 48,794
  • 16
  • 117
  • 146
fluter
  • 13,238
  • 8
  • 62
  • 100

2 Answers2

20

You may write

int64_t a = -1 - 9223372036854775807LL;

The problem is that the - is not part of the literal, it is unary minus. So the compiler first sees 9223372036854775808LL (out of range for signed int64_t) and then finds the negative of this.

By applying binary minus, we can use two literals which are each in range.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
9

Ben's already explained the reason, here's two other possible solutions.

Try this

int64_t a = INT64_MIN;

or this

int64_t a = std::numeric_limits<int64_t>::min();
john
  • 85,011
  • 4
  • 57
  • 81