I am new to C and I was looking for a custom function in C that would convert a string to an integer and I came across this algorithm which makes perfect sense except for one part. What exactly is the -'0' doing on this line n = n * 10 + a[c] - '0';?
int toString(char a[]) { int c, sign, offset, n; if (a[0] == '-') { // Handle negative integers sign = -1; } if (sign == -1) { // Set starting position to convert offset = 1; } else { offset = 0; } n = 0; for (c = offset; a[c] != '\0'; c++) { n = n * 10 + a[c] - '0'; } if (sign == -1) { n = -n; } return n; } The algorithm did not have an explanation from where I found it, here.
'0'has the value of 48,'\0'is 0 is NUL.