[英]Confusion in the output
I have just started my Data types revised chapter. 我刚刚开始修改数据类型一章。 I am currently studying the concept of signed and unsigned character. 我目前正在研究有符号和无符号字符的概念。 My doubt is that the signed char has a range from -128 to 127, then why the below code is still running ? 我的怀疑是签名的char的范围是-128到127,那么为什么下面的代码仍在运行? Also, the below code is giving the infinite o/p which is not understandable to me. 另外,下面的代码给出了无限的o / p,这对我来说是无法理解的。
main( )
{
char ch ;
for ( ch = 0 ; ch <= 255 ; ch++ )
printf ( "\n%d %c", ch, ch ) ;
}
I am currently using GCC 32-bit compiler. 我目前正在使用GCC 32位编译器。 Can anyone please help me in explaining the o/p of the above code ? 谁能帮我解释一下上面代码的o / p吗?
for ( ch = 0 ; ch <= 255 ; ch++ )
If ch
is a signed character, it will start at 0 and increment to 127. Then, at the next increment, it will "wrap around" and become -128. 如果ch
是带符号的字符,它将从0开始并递增到127。然后,在下一个增量,它将“环绕”并变为-128。 Using an unsigned char
: 使用未签名的 char
:
127 = 0x7F
128 = 0x80
But, using a signed char, 0x80
becomes -128. 但是,使用带符号的字符, 0x80
变为-128。
So now ch
will run from -128 through 127. And since all of those values are less than 255, this will repeat until you stop the program.. 因此,现在ch
将在-128到127之间运行。由于所有这些值均小于255,因此将重复该操作,直到停止程序为止。
因为带符号的字符是从-128到127,它的二进制数是10000000和01111111,当“ ch”运行到127时,下一个增量“ ch”将变为-128,始终小于255,因此它将是无限的o / p。
You are probably confused with the output. 您可能对输出感到困惑。 I think in the o/p you are seeing something like this. 我认为在o / p中您会看到类似的内容。
0 0
1 1
2 2
3 ... 3 ...
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32... 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ...
33 ! 33! 34 " .... 125 } 126 ~ 127 34英寸.... 125} 126〜127
255 256 257 255 256 257
... 511 512 513 .. and so on ... 511 512 513 ..等
0 to 32 are all flags(unprintable codes)
(hence you dont see the output , but only the numbers for the first 33), followed by characters till 127
. 0 to 32 are all flags(unprintable codes)
(因此看不到输出,但只有前33个数字), followed by characters till 127
。 As you can see it wraps around at every 255 characters to give you the same result but it actually stops printing characters after multiples of 127
( this is the 127 char list - http://web.cs.mun.ca/~michael/c/ascii-table.html ) . 如您所见,它每隔255个字符会自动换行,以提供相同的结果, but it actually stops printing characters after multiples of 127
(这是127个字符列表-http: //web.cs.mun.ca/~michael/ c / ascii-table.html )。 It just resets after 127 to -128, so the program continues to print numbers to infinity even though it is resetting the char. 它只是在127之后重置为-128,因此即使重置了char,该程序仍继续将数字打印为无穷大。 This is because when you do printf("%d",ch) for -127 it prints 128
, and so on until ch = 255 and then it flips again and starts printing 256 onwards and so on but the actual ch value never goes above 127 and hence it goes to infinity 这是因为当您printf("%d",ch) for -127 it prints 128
执行printf("%d",ch) for -127 it prints 128
,依此类推,直到ch = 255,然后再次翻转并开始打印256,依此类推,但是实际的ch值永远不会超过127,因此达到无穷大
A signed char c
should you be giving you the above output. 如果您要提供上述输出,请使用已signed char c
。 A char is essentially an integer 8 bits wide, but by default probably signed on your compiler. 一个char本质上是一个8位宽的整数,但是默认情况下可能在您的编译器上签名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.