Quite often in the code you can see strings like

char cdigit = '8'; int idigit = cdigit - '0'; 

Rarer, but also occurs

 char letter = 'd'; int letter_number = letter - 'a'; 

We open the Straustrup "C ++ programming language" special edition, "Binom-Press", 2008. Quote (page 110):

It is unsafe to assume ... that the characters of the alphabet are continuous (in the EBCDIC standard there is a gap between i and j)

For Kernighan and Ritchie, the above code (at least in numbers) is met regularly.

Actually the question on the numbers: is the idigit = cdigit - '0' code idigit = cdigit - '0' ?

  • Actually found stackoverflow.com/questions/868496/… . It states that the current standard C guarantees correctness. Answer from 09 year. - andy.37
  • Thank you so much for the quotes from the standards. There is one problem - I am confused, who should put a tick) Why can't I put two? - andy.37
  • And one answer on C, and the second on C ++ :) - VladD
  • @VladD, and the quotes match the letter character)) - andy.37
  • Someone can throw a link to the implementation of atoi . I wonder how it is written. I found only heders. - andy.37

2 answers 2

The standard C ++ §2.3 / 4 reads as follows:

The number of decals digits should be greater than the value of each.

i.e

In both the source and runtime character sets, the value of each character after 0 in the above list of decimal digits must be one greater than the previous one.

(my translation). The set of numbers given in §2.3 / 1 is as follows:

0 1 2 3 4 5 6 7 8 9

This guarantees a sequence of digit code values.

  • Well, and I understand that the letter -'a' need to beat his hands? - andy.37
  • @ andy.37: I didn’t find anything in the standard about the letter , so in theory, yes, I need to beat my hands. - VladD
  • one
    @ andy.37: Plus EBCDIC is a clear counterexample. - VladD

Still found such a phrase in sishnom Standard (C99):

The decimal digits should be longer than the value of each.