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!
Asked
Active
Viewed 195 times
1
-
`10000000000` is larger than an `int` you need to use a larger data type – Alan Birtles Jul 14 '21 at 06:44
-
1If you enabled [compiler warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) you would probably have seen the problem right away. – n. m. could be an AI Jul 14 '21 at 06:59
-
Understood, thanks both a lot! – HVLarry Jul 15 '21 at 03:36
2 Answers
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.

463035818_is_not_an_ai
- 109,796
- 11
- 89
- 185

Michael Chourdakis
- 10,345
- 3
- 42
- 78
-
1paradocslover is not a factual sentence, but yours is. Anyways, i found the answer on your profile. So nvm – paradocslover Jul 14 '21 at 06:45
-
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