The following code is from K&R textbook, page number 71:
val =10.0*val+s[i] -'0'
What does s[i] -'0'
mean here?
cc-stringschardigits
The following code is from K&R textbook, page number 71:
val =10.0*val+s[i] -'0'
What does s[i] -'0'
mean here?
Best Answer
It seems that
s
is a character array or a pointer to the first element of a character array. And elements[i]
contains a character that represents a digit as for example'5'
. To convert this character that for example in ASCII has internal code equal to53
(while internal code of character'0'
is 48) there is used expressionthat is equivalent to
and equal to number
5
According to the C Standard (5.2.1 Character sets)
So in ASCII there is a relation
Character Code value
For example in EBCDIC internal codes of characters that represent digits are
240 - 249
( 0 - 9 ) or in hexadecimal notationF0 - F9
.So it is a standard way to get numeric digit from a character independing on used character set.