### whats does '0' means in this code?

can anyone tell me whats the meaning of `'0'` in this code?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````string add (string &s1, string &s2) { int carry=0,sum; string min=s1, max=s2, result = ""; if (s1.length()>s2.length()) { max = s1; min = s2; } else { max = s2; min = s1; } for (int i = min.length()-1; i>=0; i--) { sum = min[i] + max[i + max.length() - min.length()] + carry - 2*'0'; carry = sum/10; sum %=10; result = (char)(sum + '0') + result; } i = max.length() - min.length()-1; while (i>=0) { sum = max[i] + carry - '0'; carry = sum/10; sum%=10; result = (char)(sum + '0') + result; i--; } if (carry!=0) { result = (char)(carry + '0') + result; } return result; }``````

Last edited on
'0' is the ASCII character for zero.
It is a character which represents the digit zero.
Internally, each character can be represented by a number, for example 'A' is 65 and '0' is 48 in the ASCII code.

The reason why it is used is to convert to/from ordinary text and an actual number which can be used in computations.
so why does it have to put '0' while you can do arithmetic operations with just a normal integer 0 (w/o quotation)?

in line 41....does it mean you are adding the sum of the character zero and carry to result?
 '0' is the ASCII character for zero.

Well, more generally, '0' is the character zero. It needn't be ASCII, if the operating system uses some other encoding, such as EBCDIC.
 so why does it have to put '0' while you can do arithmetic operations with just a normal integer 0 (w/o quotation)?

Because integers ans string are represented differently.
`result` is a string, so you want the digits you store into it to be in character representation. The decimal value of ASCII '0' is 48. So you have to bias the integer digits you store into result by that.

 in line 41....does it mean you are adding the sum of the character zero and carry to result?

carry and '0' are converted to a character value, then that character value and the previous result string are concatentate and stored in result.
`'0'` is the integral offset into the ASCII table where the numbers are represented. To turn an ASCII number ('0' - '9') into its decimal representation (0 - 9) you can subtract the offset.

For example, `'1' - '0' == 1`.

Last edited on
ahh..now i get it :) thank you very much for the help :)
Topic archived. No new replies allowed.