关于字符操作的查询
请解释下面的代码。
printf("%c\n",0+'0'); --> returns 0
printf("%c\n",1+'0'); --> returns 1
printf("%c\n",0+'1'); --> returns 1
printf("%c\n",1+'1'); --> returns 2
谢谢。
Please explain the following piece of code..
printf("%c\n",0+'0'); --> returns 0
printf("%c\n",1+'0'); --> returns 1
printf("%c\n",0+'1'); --> returns 1
printf("%c\n",1+'1'); --> returns 2
Thanx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 ASCII 表。 “0”的代码为 48。因此“0”+1 得到 49,即“1”。所以每个字符实际上都是一个整数。您向其中添加另一个整数,然后,因为您在 printf 中指定了“%c”,所以强制它将其视为字符。他检查了 ASCII 表,经过深思熟虑后,他决定将输出打印到屏幕上。
Look at the ASCII table. '0' has code 48. So '0' + 1 yields 49, which is '1'. So every character is in fact an integer. You add another integer to it and then, because you specify "%c" in printf, you force it to consider it a character. He goes check his ASCII table and, after some deliberation he decides to print the output to the screen.
'0'
给出字符0
的ASCII
值,即48
。添加0
即可得到48
。然后将48
作为一个字符打印回来,得到0
类似地,下一个将
1
添加到48
得到 < code>49 当打印为 char 时给出1
'0'
gives theASCII
value of char0
which is48
. To that you add0
to get48
. Then you print48
back as a character which gives0
Similarly the next one adds
1
to48
to give49
which when printed as char gives1
感谢
%c
它们都打印了与参数等效的字符。将零添加到字符零的 ASCII 值,即 48:48 + 0 = 48。
尝试
printf("%d\n", '0');
获取 ASCII 值。Thanks to
%c
all of them print the character equivalent of the argument.Adds zero to the ASCII value of the character zero which is 48: 48 + 0 = 48.
Try
printf("%d\n", '0');
to get the ASCII value.