I have two integers, m and k. The product of m and k might overflow an int datatype, so I used long long int data type but I was still getting runtime error:

signed integer overflow: 89945 * 32127 cannot be represented in type 'int' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:14:28.

However when I used,

long long int b = static_cast<long long>(m) * static_cast<long long>(k);

it worked! So my doubt is why is casting m and k into long long int mandatory here?

0

There are 0 best solutions below