#include <stdio.h>
int main()
{ char i=0;
for(;i>=0;i++);
printf("%d",i);
return 0;
}
The output of this program is -128. As far as i understood the character variable will overflow and all bits will be 0. And taking two's complement will be again 0. Can somebody explain the process?
该程序的输出为-128。据我所知,字符变量将溢出,所有位将为0.并且取两个补码将再次为0.有人可以解释这个过程吗?
EDIT : Just to clarify that this is not my program. It is a programming question in a competitive exam.
编辑:只是为了澄清这不是我的计划。这是竞争性考试中的编程问题。
3 个解决方案
#1
5
Your assumption about overflowing isn't correct. Overflowing an unsigned type will set all bits to zero, but overflowing a signed type is undefined, so it could result in any value.
关于溢出的假设是不正确的。溢出无符号类型会将所有位设置为零,但是未定义溢出有符号类型,因此可能导致任何值。
What you get as a result therefore depends on your (compiler) implementation. It could be explained if
因此,您获得的结果取决于您的(编译器)实现。可以解释一下
- Your implementation has signed
char
. - A
char
has 8 bits. - Negative numbers are represented in 2's complement.
- Overflow of a signed value will cause wraparound. (which is the consequence of a simple implementation using 2's complement, see below)
您的实现已签署char。
char有8位。
负数用2的补码表示。
签名值的溢出将导致环绕。 (这是使用2的补码进行简单实现的结果,见下文)
Given all these assumptions (and remember, none of these are specified by C), incrementing 127
as the maximum possible char
value (represented as 0111 1111
) yields -128
, the minimum possible value (represented as 1000 0000
).
给定所有这些假设(并且记住,这些假设都没有由C指定),将最大可能的char值(表示为0111 1111)递增127得到-128,即最小可能值(表示为1000 0000)。
TL;DR -- your code is undefined, don't write such code.
TL; DR - 您的代码未定义,请勿编写此类代码。
#2
0
Actually at each iteration, loop will increment i
by 1
, which lead us to i = 127
.
实际上在每次迭代时,循环将i递增1,这导致我们i = 127。
- Now, lets look its binary representation
0111-1111
. - Now if we increment it by
1
.Then -
0111-1111
+0000-0001
=1000-0000
- Now, analyse this, MSB is set, which means it is a negative number
- Negative numbers are save as 2's complement
- 2's complement of
1000-0000
=-127
- Hence loop breaks
现在,让我们看看它的二进制表示0111-1111。
现在如果我们将它增加1.然后
0111-1111 + 0000-0001 = 1000-0000
现在,分析一下,MSB设置,这意味着它是一个负数
负数保存为2的补码
2的补码1000-0000 = -127
因此循环中断
Hope you will get it.
希望你能得到它。
#3
0
Since your char is SIGNED it can be between -128 and 127 (in 8 bits) and in that for you are incrementing until it gets to max value and then overflows and becomes the - value since that's how negative value is written in bits. Take a look at the following pic:
由于你的char是SIGNED,它可以在-128和127之间(以8位为单位),并且因为你正在递增直到它达到最大值然后溢出并变成 - 值,因为这是负值以位写入的方式。看看下面的图片:
p.s. with some printing inside the loop you could figure it out yourself too.
附:在循环内部进行一些打印,您也可以自己解决。
#1
5
Your assumption about overflowing isn't correct. Overflowing an unsigned type will set all bits to zero, but overflowing a signed type is undefined, so it could result in any value.
关于溢出的假设是不正确的。溢出无符号类型会将所有位设置为零,但是未定义溢出有符号类型,因此可能导致任何值。
What you get as a result therefore depends on your (compiler) implementation. It could be explained if
因此,您获得的结果取决于您的(编译器)实现。可以解释一下
- Your implementation has signed
char
. - A
char
has 8 bits. - Negative numbers are represented in 2's complement.
- Overflow of a signed value will cause wraparound. (which is the consequence of a simple implementation using 2's complement, see below)
您的实现已签署char。
char有8位。
负数用2的补码表示。
签名值的溢出将导致环绕。 (这是使用2的补码进行简单实现的结果,见下文)
Given all these assumptions (and remember, none of these are specified by C), incrementing 127
as the maximum possible char
value (represented as 0111 1111
) yields -128
, the minimum possible value (represented as 1000 0000
).
给定所有这些假设(并且记住,这些假设都没有由C指定),将最大可能的char值(表示为0111 1111)递增127得到-128,即最小可能值(表示为1000 0000)。
TL;DR -- your code is undefined, don't write such code.
TL; DR - 您的代码未定义,请勿编写此类代码。
#2
0
Actually at each iteration, loop will increment i
by 1
, which lead us to i = 127
.
实际上在每次迭代时,循环将i递增1,这导致我们i = 127。
- Now, lets look its binary representation
0111-1111
. - Now if we increment it by
1
.Then -
0111-1111
+0000-0001
=1000-0000
- Now, analyse this, MSB is set, which means it is a negative number
- Negative numbers are save as 2's complement
- 2's complement of
1000-0000
=-127
- Hence loop breaks
现在,让我们看看它的二进制表示0111-1111。
现在如果我们将它增加1.然后
0111-1111 + 0000-0001 = 1000-0000
现在,分析一下,MSB设置,这意味着它是一个负数
负数保存为2的补码
2的补码1000-0000 = -127
因此循环中断
Hope you will get it.
希望你能得到它。
#3
0
Since your char is SIGNED it can be between -128 and 127 (in 8 bits) and in that for you are incrementing until it gets to max value and then overflows and becomes the - value since that's how negative value is written in bits. Take a look at the following pic:
由于你的char是SIGNED,它可以在-128和127之间(以8位为单位),并且因为你正在递增直到它达到最大值然后溢出并变成 - 值,因为这是负值以位写入的方式。看看下面的图片:
p.s. with some printing inside the loop you could figure it out yourself too.
附:在循环内部进行一些打印,您也可以自己解决。