关于字符操作的查询

发布于 2024-08-31 12:52:16 字数 222 浏览 4 评论 0原文

请解释下面的代码。

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

这个俗人 2024-09-07 12:52:16

查看 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.

鸠书 2024-09-07 12:52:16

'0' 给出字符 0ASCII 值,即 48。添加 0 即可得到 48。然后将 48 作为一个字符打印回来,得到 0

类似地,下一个将 1 添加到 48 得到 < code>49 当打印为 char 时给出 1

'0' gives the ASCII value of char 0 which is 48. To that you add 0 to get 48. Then you print 48 back as a character which gives 0

Similarly the next one adds 1 to 48 to give 49 which when printed as char gives 1

谷夏 2024-09-07 12:52:16

感谢 %c 它们都打印了与参数等效的字符。

printf("%c\n",0+'0');

将零添加到字符零的 ASCII 值,即 48:48 + 0 = 48。
尝试 printf("%d\n", '0'); 获取 ASCII 值。

printf("%c\n",1+'0'); // 1 + 48 = 49 which is the character `1`
printf("%c\n",0+'1'); // 0 + 49 which is again `1`
printf("%c\n",1+'1'); //left as an exercise

Thanks to %c all of them print the character equivalent of the argument.

printf("%c\n",0+'0');

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.

printf("%c\n",1+'0'); // 1 + 48 = 49 which is the character `1`
printf("%c\n",0+'1'); // 0 + 49 which is again `1`
printf("%c\n",1+'1'); //left as an exercise
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文