简体   繁体   English

当超出C的整数范围时会发生不确定循环

[英]Indefinite loop occurs when out of integer range in C

My question is that why an indefinite loop occurs when we try to print numbers from 0 to 32767, 我的问题是,当我们尝试打印0到32767之间的数字时,为什么会发生不确定的循环,

why does it goes back to -32767 and restarts printing. 为什么返回到-32767并重新开始打印。

PROGRAM CODE- 程序代码

main(){

         int i;

         for(i=0; i<=32767; i++){

         printf("%d\n", i);

    }
}

It seems that maximim value of an object of type int ( INT_MAX ) in your environment is equal to 32767. Thus then i is equal to 32767 在您的环境中,似乎类型为int( INT_MAX )的对象的INT_MAX等于32767。因此,我等于32767

for(i=0; i<=32767; i++){

after evaluation expression i++ the sign bit of the internal representation of i is set and i becomes equal to negative value -32768 that is to INT_MIN In this case condition 在评估表达式i++之后, i++的内部表示的符号位被设置,并且i等于等于INT_MIN负值-32768。

i<=32767

will yield true. 将产生真实的。

Because 32767 is the maximum value for a 16-bit int. 因为32767是16位int的最大值。

What else would you expect its value to be after incrementing that by one? 将其值增加一之后,您还希望它的值是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM