In convertToInt()

why we subtracted ‘0’ from the char

Because the C++ standard guarantees that the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 are always in this order regarding their numerical character code. So, if you subtract the char code of ‘0’ from another digit, it will give its position relative to 0 , which is its value

let take one example

char ch='9';

int x = '9' - '0';

0 was 48, 1 will be 49, 2 will be 50 etc… in ASCII,
then x would contain, ascii value of ‘9’ minus the ascii value of ‘0’ which means, ascii value of ‘9’ would be 57 and hence, x would contain 57 - 48 = 9.

hence in this way we can get integer for char

i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved :grinning:
if you have more doubts regarding this feel free to ask

1 Like