当我为一个int分配一个大于INT_MAX的数字时会发生什么? [英] What happens when I assign a number larger than INT_MAX to an int?
问题描述
假设我将一个11位数分配给一个整数,会发生什么?我玩了一点,我知道它给了我一些int范围内的数字。如何创建这个新数字?
Suppose I assign an eleven digits number to an int, what will happen? I played around with it a little bit and I know it's giving me some other numbers within the int range. How is this new number created?
推荐答案
这是实现定义的行为。这意味着您的编译器必须提供说明在这种情况下会发生什么情况的文档。
It is implementation-defined behaviour. This means that your compiler must provide documentation saying what happens in this scenario.
因此,请查阅该文档以获取答案。
So, consult that documentation to get your answer.
实现的一种常见定义方式是将输入整数截断为 int
的位数(在必要时将unsigned视为有符号后重新解释)。
A common way that implementations define it is to truncate the input integer to the number of bits of int
(after reinterpreting unsigned as signed if necessary).
C ++ 14标准参考:[expr.ass] / 3,[conv.integral] / 3
C++14 Standard references: [expr.ass]/3, [conv.integral]/3
这篇关于当我为一个int分配一个大于INT_MAX的数字时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!