You need to do - '0'
operation if and only if you need to use the char
as an int
Example:
char c = '7';
int a = c;
If you print the integer with its integel value,
printf("%d", a);
you'd get 55
. That is because the value in the memory of the int
contains '7'
in the memory, and it's int value will be 55
in ASCII table.
If you print the integer using char, it takes the char value and gives you 7
printf("%c", a);
Note that, while printing the formats must be fitting, for example you can't print float value %f
on an int, because float on print expects double
, due to more memory being required. (Double is 8 bytes, hence int and char are 4 byte values)
The result of operation char - char
will be integer in the case of the char's being numericals. So in range of 0
to 9
.
So if you were to print
printf("%d", '9' - '2');
This will give you 7
as an an integer and not char because the result of that equation result into an int
.
So, substracting basic '0'
char from a char is an easy way to get an integer value in return, leaving the char being substracted in its original form.