What happens if I assign a negative value to an unsigned variable?

前端 未结 6 912
旧巷少年郎
旧巷少年郎 2020-11-22 07:31

I was curious to know what would happen if I assign a negative value to an unsigned variable.

The code will look somewhat like this.

unsigned int nVa         


        
6条回答
  •  遇见更好的自我
    2020-11-22 08:25

    You're right, the signed integer is stored in 2's complement form, and the unsigned integer is stored in the unsigned binary representation. C (and C++) doesn't distinguish between the two, so the value you end up with is simply the unsigned binary value of the 2's complement binary representation.

提交回复
热议问题