I know that to convert any given char to int, this code is possible [apart from atoi()]:
int i = '2' - '0';
but I never understood how it worked, what is significance of ‘0’ and I don’t seem to find any explanation on the net about that.
Thanks in advance!!
In C, a character literal has type
int. [Character Literals/IBM]In your example, the numeric value of
'0'is 48, the numeric value of'2'is 50. When you do'2' - '0'you get50 - 48 = 2. This works for ASCII numbers from 0 to 9.See ASCII table to get a better picture.
Edit: Thanks to @ouah for correction.