why overflow happens on calculation depending on the data type when the type where the value is being assigned can hold it

前端 未结 5 2091
别那么骄傲
别那么骄傲 2021-01-28 02:10

Earlier I came up with something, which I solved, but it got me later let\'s take a look at a similar example of what I was on:

int b = 35000000; //35million
int         


        
5条回答
  •  天命终不由人
    2021-01-28 02:28

    The maximum number that can be represented in a 32-bit signed integer without overflow is 2147483647. 100*30000000 is larger than that.

    The type of an arithmetic operation is completely independent of the type of the variable you're storing it into. It's based on the type of the operands. If both operands are of type int, the result will be of type int too, and that result will then be converted before it is stored in the variable.

提交回复
热议问题