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_an_ai
  • 109,796
  • 11
  • 89
  • 185
Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
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
  • 6,130
  • 3
  • 25
  • 42