1

Can someone explain me this line like I'm five ? I'm not kidding, I just don't understand why we're multiplying 0 by 10. Thanks in advance.

Comments
  • 2
    After the first iteration the number will be greater than 0 so the multiplication will actually do something. But for the first round it basically does nothing until the *p - 0 is added to it.
  • 1
    For the sake of simplicity. This atoi function (ascii to integer) will take a string and convert the ascii representation of a number and convert it to an integer (will stop as soon a char in the string is not an ascii number). The number variable will be the resulting integer value and for each iteration the char digit, or its ascii representation, will be multiplied by a factor of 10. The first digit is not a multiple of a factor of 10, well, it is but it's 10^0 which is one, that is the reason why the author of that function implemented it that way. Simple
  • 1
    You know,, a char digit is in hex 0x30 - 0x39. So, it means *p - '0' equals to chr(ord(*p) - ord('0'))
Add Comment