1

I can't print 100000 * 100000 when I use COUT in C++. The output was 1410065408 instead of 10000000000. How can I fix that? Thanks!

HVLarry
  • 13
  • 2

2 Answers2

5

By default, integer literals are of type int which causes the overflow you have. Use 100000LL to mark the number as long long and have a long long result.

https://en.cppreference.com/w/cpp/language/integer_literal

463035818_is_not_a_number
  • 88,680
  • 9
  • 76
  • 150
Michael Chourdakis
  • 9,693
  • 2
  • 38
  • 66
1

The value is truncated since 10000000000 is larger than the default interger type int max value (std::numeric_limit<int>::max()) ,

hex(10000000000) = 0x2540be400
hex(1410065408) =   0x540be400

You can see that the first byte is truncated.

To fix it:

100000LL * 100000

or cast it

static_cast<int64_t>(100000) * 100000
prehistoricpenguin
  • 5,845
  • 3
  • 22
  • 38