Блог пользователя Burglar_anu

Автор Burglar_anu, история, 3 года назад, По-английски

I understand that if value exceeds the range of int it gives overflow error like

int a=100000 and int b=100000, int c=a*b then cout<<c; will output overflow error

but instead of storing the value if i just print it then why does it gives overflow error

int a=100000 and int b=100000, cout<<a*b; [submission:244431214]

Please help!

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Both a and b are int, so a*b is stored in an int. In order to not get overflow, at least one of the variables should be long long int, and C++ will automatically convert the answer to long long int. You can fix above code by using cout<< 1LL*a*b;.