user3243499 user3243499 - 3 months ago 20
C++ Question

Long integer overflow in C++

Why the below code gives integer overflow warning:

#include <stdio.h>

int main()
{
long long int x = 100000 * 99999;
return 0;
}


Whereas below code works perfectly:

#include <stdio.h>

int main()
{
long long int x = 100000000000000;
return 0;
}

Answer Source

Because here

long long int x = 100000 * 99999;

two ints are multiplied. Try

long long int x = 100000LL * 99999;