请解释一下这段代码的作用 (someChar - 48)

发布于 2024-09-08 15:52:33 字数 324 浏览 5 评论 0 原文

我正在解决一些练习问题,我看到了这段代码:

#include <stdio.h>
#include <string.h>

int main(void) {
   char* s = "357";
   int sum = 0;
   int i = 0;
   for (i = 0; i < strlen(s); i++) {
     sum += s[i] - 48;
   }
   printf("Sum is %d", sum);

   return 0;
}

有人能解释一下这段代码的作用吗,特别是 48 部分的减法?

I'm going through some practice problems, and I saw this code:

#include <stdio.h>
#include <string.h>

int main(void) {
   char* s = "357";
   int sum = 0;
   int i = 0;
   for (i = 0; i < strlen(s); i++) {
     sum += s[i] - 48;
   }
   printf("Sum is %d", sum);

   return 0;
}

Can someone explain what the code does, especially the subtraction with 48 part?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

千柳 2024-09-15 15:52:34

尝试初始化不带单引号的变量。与 ASCII 不同,这应该给出准确的值

Try initializing the variable without single quotes . That should give the exact value unlike ASCII

暗藏城府 2024-09-15 15:52:33

该代码基本上将表示为字符串的数字的数字相加。它做出了两个重要的假设才能正常工作:

  • 字符串仅包含 '0'..'9' 范围内的
  • 字符 使用的字符编码是 ASCII

在 ASCII 中,'0' == 48 '1' == 49 等等。因此,'0' - 48 == 0'1' - 48 == 1,依此类推。也就是说,减去 48 会将 char'0'..'9' 转换为 int0..9

因此,正是因为 '0' == 48,该代码也将适用于:

sum += s[i] - '0';

在此版本中,意图可能稍微更清晰一些。

当然,您可以通过加法进行“反向”映射,例如 5 + '0' == '5'。同样,如果您的 char 包含 'A'..'Z' 范围内的字母,则可以“减去”'A'从中获取该字母在 0..25 范围内的索引。

另请参阅

相关问题


关于替代编码

如前所述,原始 - 48 代码假定使用的字符编码是 ASCII。 - '0' 不仅提高了可读性,而且放弃了 ASCII 假设,并且可以与 任何 编码一起使用,正如 C 语言所规定的,它规定数字字符必须是在连续块中按顺序编码。

另一方面,对于信件却没有这样的规定。因此,在使用 EBCDIC 编码的极少数情况下,例如,将 'A'..'Z' 映射到 0..25 不再那么简单减去 'A',因为字母不是在 EBCDIC 中的连续块中按顺序编码。

一些编程语言通过强制使用一种特定的编码来表示源代码来简化问题(例如,Java 使用 Unicode:JLS §3.1)

另请参阅

相关问题

The code basically sums the digits of a number represented as a string. It makes two important assumptions to work properly:

  • The string contains only chars in the '0'..'9' range
  • The character encoding used is ASCII

In ASCII, '0' == 48, '1' == 49, and so on. Thus, '0' - 48 == 0, '1' - 48 == 1, and so on. That is, subtracting by 48 translates the char values '0'..'9' to the int values 0..9.

Thus, precisely because '0' == 48, the code will also work with:

sum += s[i] - '0';

The intention is perhaps slightly more clear in this version.

You can of course do the "reverse" mapping by addition, e.g. 5 + '0' == '5'. Similarly, if you have a char containing a letter in 'A'..'Z' range, you can "subtract" 'A' from it to get the index of that letter in the 0..25 range.

See also

Related questions


On alternative encodings

As mentioned, the original - 48 code assumes that the character encoding used is ASCII. - '0' not only improves readability, but also waives the ASCII assumption, and will work with any encoding, as specified by the C language which stipulates that digit characters must be encoded sequentially in a contiguous block.

On the other hand, no such stipulation is made about letters. Thus, in the rare situation where you're using EBCDIC encoding, for example, mapping 'A'..'Z' to 0..25 is no longer as simple as subtracting 'A', due to the fact that letters are NOT encoded sequentially in a contiguous block in EBCDIC.

Some programming languages simplify matters by mandating one particular encoding is used to represent the source code (e.g. Java uses Unicode: JLS §3.1)

See also

Related questions

纵性 2024-09-15 15:52:33

求字符串 s 中数字的总和。

sum += s[i] - 48; 将 ASCII 字符转换为其数值。

Finding the sum of the numbers in the string s.

The sum += s[i] - 48; converts ASCII characters to their numeric values.

诗化ㄋ丶相逢 2024-09-15 15:52:33

将 3 + 5 + 7 加起来,然后打印

总和是15

-48 部分是减去字符 0,即 0 的 ascii 值。

所以它的作用是

'3' - '0' > 51 - 48
'5' - '0' > 53 - 48
'7' - '0' > 55 - 48

如你所见,在 C 中,'0'(字符零)与 0(数字 0)不同。他们有不同的价值观(除其他外)

its adding up 3 + 5 + 7, and then printing

Sum is 15

The -48 part is that it is subtracting the character 0, that is, the ascii value for 0.

So what it does is

'3' - '0' > 51 - 48
'5' - '0' > 53 - 48
'7' - '0' > 55 - 48

As you can see, in C, '0' (character zero) is different from 0 (number 0). They have different values (amongst other things)

半岛未凉 2024-09-15 15:52:33

我建议编写一个测试程序来查看 s[] 的值显示什么。您还可以打印“0123456789”中每个条目的所有值。

我想您很快就会意识到它在做什么,尽管此代码依赖于 ASCII 编码。

玩得开心!

I suggest writing a test program to see what the values of s[] display. You might also print out all the values for each entry in "0123456789".

I think you'll quickly realize what it's doing, although this code is relying on ASCII encoding.

Have fun!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文