请解释这段代码在做什么(someChar - 48) [英] Please explain what this code is doing (someChar - 48)
问题描述
我正在经历一些练习问题,我看到这个代码:
#include< stdio.h>
,代码也将使用:
#include< string.h>
int main(void){
char * s =357;
int sum = 0;
int i = 0;
for(i = 0; isum + = s [i]
}
printf(Sum is%d,sum);
返回0;有人可以解释一下代码的作用,特别是48部分的减法? / p>
解决方案代码基本上总是表示为字符串的数字的数字。它使两个重要的假设正常工作:
- 字符串只包含
'0' 9'
范围
- 所用的字符编码为ASCII
在ASCII中,
'0'== 48
,'1'== 49
,等等。因此,'0' - 48 == 0
,'1' - 48 == 1
,等等。也就是说,减去48将char
值转换为'0'..'9'
c> int values0..9
。
c $ c>'0'== 48
sum + = s [i] - '0';
这个版本的意图可能稍微清楚一些。
你当然可以通过加法进行反向映射,例如
5 +'0'=='5'
。同样,如果您有char
在'A'..'Z'
范围中包含一个字母,减去'A'
,以在0..25
范围内获取该字母的索引。
另请参阅
相关问题
- 如何将单个字符转换为整数
- 语言showdown:将字符串数字转换为整数数组?
- 很多例子这个数字转换,使用减法
'0'
和48
!
- 很多例子这个数字转换,使用减法
替代编码
$ b b
如前所述,原始的 - 48
代码假定所使用的字符编码是ASCII。 - '0'
不仅提高了可读性,而且放弃了ASCII假设,并且可以使用C语言指定的任何编码规定数字字符必须在连续块中按顺序编码。
另一方面,没有对字母做出这样的规定。因此,在使用EBCDIC编码的罕见情况下,例如,将'A'..'Z'
映射到 0..25
不再象'A'
那样简单,因为字母在
有些编程语言通过强制使用一个特定的编码来表示源代码(例如Java使用Unicode: JLS§3.1)
另请参阅
相关问题
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?
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
- How to convert a single char into an int
- Language showdown: Convert string of digits to array of integers?
- Many examples of this digit conversion, using subtraction with both
'0'
and48
!
- Many examples of this digit conversion, using subtraction with both
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
这篇关于请解释这段代码在做什么(someChar - 48)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!