请解释一下这段代码的作用 (someChar - 48)
我正在解决一些练习问题,我看到了这段代码:
#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 部分的减法?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试初始化不带单引号的变量。与 ASCII 不同,这应该给出准确的值
Try initializing the variable without single quotes . That should give the exact value unlike ASCII
该代码基本上将表示为字符串的数字的数字相加。它做出了两个重要的假设才能正常工作:
'0'..'9'
范围内的在 ASCII 中,
'0' == 48
、'1' == 49
等等。因此,'0' - 48 == 0
、'1' - 48 == 1
,依此类推。也就是说,减去 48 会将char
值'0'..'9'
转换为int
值0..9
。因此,正是因为
'0' == 48
,该代码也将适用于:在此版本中,意图可能稍微更清晰一些。
当然,您可以通过加法进行“反向”映射,例如
5 + '0' == '5'
。同样,如果您的char
包含'A'..'Z'
范围内的字母,则可以“减去”'A'
从中获取该字母在0..25
范围内的索引。另请参阅
相关问题
'0'
和48
进行减法!关于替代编码
如前所述,原始
- 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:
'0'..'9'
rangeIn ASCII,
'0' == 48
,'1' == 49
, and so on. Thus,'0' - 48 == 0
,'1' - 48 == 1
, and so on. That is, subtracting by 48 translates thechar
values'0'..'9'
to theint
values0..9
.Thus, precisely because
'0' == 48
, the code will also work with: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 achar
containing a letter in'A'..'Z'
range, you can "subtract"'A'
from it to get the index of that letter in the0..25
range.See also
Related questions
'0'
and48
!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'
to0..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
求字符串 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.将 3 + 5 + 7 加起来,然后打印
-48
部分是减去字符 0,即 0 的 ascii 值。所以它的作用是
如你所见,在 C 中,'0'(字符零)与 0(数字 0)不同。他们有不同的价值观(除其他外)
its adding up 3 + 5 + 7, and then printing
The
-48
part is that it is subtracting the character 0, that is, the ascii value for 0.So what it does is
As you can see, in C, '0' (character zero) is different from 0 (number 0). They have different values (amongst other things)
我建议编写一个测试程序来查看 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!