0

I'm new to programming and sometimes see expressions like this

...
for (int i=0; i<str1.length(); i++) 
{ 
    int sum = ((str1[i]-'0')+(str2[i]-'0')); 
    str.push_back(sum%10 + '0'); 
}
...

So that is '0' here? Is it some kind of converting or something?

StarLight
  • 59
  • 7
  • 2
    if `str2[i]` is some *digit*, say `'5'` or `'9'` then `str2[i]-'0'` returns its *integer value*: `'3' -> 3` etc. – Dmitry Bychenko Dec 21 '21 at 16:46
  • This is a bit of a hack that converts a value between 0 and 9 to the equivalent character. I suspect this comes from C++ code because there's no `push_back` in any .NET container and worse, to get a string out of those characters you have to allocate a new string in memory. Any benefit you may have gained by the hack will be lost – Panagiotis Kanavos Dec 21 '21 at 16:49
  • 1
    Are you sure this is C#, as opposed [to C++](https://www.cplusplus.com/reference/string/string/push_back/)? – GSerg Dec 21 '21 at 16:49
  • 1
    It's far more likely this is actually C++ code, appending characters to a `std::string`. That class does have a `push_back`. – Panagiotis Kanavos Dec 21 '21 at 16:51
  • So, every answer is now invalid since you changed the tag to `c++` instead of `c#`? – Rand Random Dec 21 '21 at 17:01
  • 2
    @RandRandom the logic remains the same. It works for any strongly typed languages that differenciates `char` from `string` and uses the ASCII table. It works for C, C++, C#, probably Java, and certainly many others – Cid Dec 21 '21 at 17:02
  • Does this answer your question? [Why does subtracting '0' in C result in the number that the char is representing?](https://stackoverflow.com/questions/15598698/why-does-subtracting-0-in-c-result-in-the-number-that-the-char-is-representing) – GSerg Dec 21 '21 at 17:05
  • @Cid -- it works for every valid character encoding, not just ASCII; C and C++ require that the characters `'0' .. '9'` be contiguous and increasing. – Pete Becker Dec 21 '21 at 18:55

5 Answers5

4

It's literally the character zero '0'

The operation str[i] - '0' is used to convert the representation of a digit into its numeric value.

Since the characters from '0' to '9' are following in the ascii table, having respectively, the values 48 to 57, when you perform the operation '3' - '0', the interpreter will use the ascii values, 51 - 48 == 3, so you can convert '3' to 3

ASCII Table

enter image description here

(source of the picture : Wikipedia)

Cid
  • 14,968
  • 4
  • 30
  • 45
  • 1
    Notable that this works with other character code Tabletten like _EBCDIC_ as well. – πάντα ῥεῖ Dec 21 '21 at 17:15
  • @πάνταῥεῖ I don't know other tables, if you think you can rephrase my answer so it's more generic, with more examples, please feel free to do so – Cid Dec 21 '21 at 22:40
2

'0' is the character zero. Since characters are sequential, adding a digit to 0 will produce the character representing that digit (once cast back to a char). E.g., (char)(2 + '0') (i.e., the integer two plus the character zero) will produce '2' (i.e., the character two).

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • doesnt seem to produce the `character two`, as I assumed this `Console.WriteLine((2 + '0').GetType());` will print `System.Char` but it doesn't see here: https://dotnetfiddle.net/1pNK0d – Rand Random Dec 21 '21 at 16:56
  • @RandRandom eap, my bad - you need to cast it back to a `char`, neglected to mention that: https://dotnetfiddle.net/9X33o9 (edited into my answer too) – Mureinik Dec 21 '21 at 16:59
1

Well

str2[i] - '0'

Converts character representation of a digit ('3', '7', '9') into its integer value (3, 7, 9). More accurate (and more wordy) construction is

(int)char.GetNumericValue(str2[i])
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • So `char` math operation ( -, + I guess ) `char` "transforms" to `integer`? – Rand Random Dec 21 '21 at 16:50
  • @Rand Random: every `char` is in fact some integer (so called **ascii code**), however, ascii code of `'0'` is `48`, `'1'` is `49` etc. If we want to get `0` for `'0'`, `5` for `'5'` we have to normalize and what we do by subtracting `'0'` (or `48`) – Dmitry Bychenko Dec 21 '21 at 16:55
  • Yeah, but I didn't assume it would change the type, thought it would still be of type `char` - this was the unknown part to me – Rand Random Dec 21 '21 at 16:57
  • 1
    @Rand Random: when we do arithmetics (say, subtract) with `char`s, the result will be `int` – Dmitry Bychenko Dec 21 '21 at 17:00
1

What is implied, but not really stated, in the other answers is that int and char can be treated as fairly equivalent in c#. You can do math on chars just like you can on ints, and you can convert back and forth between int and char with ease. The number you get is based on the position in the utf8 character tables for the char.

'0' as a char has an int value of 48, so you could do:

int x = 7;
char c = x+48; //c would be '7' as a char, or 55 as an int

Other examples:

char c = 'a';

c++;

Console.Write(c); //prints 'b', because 'a' + 1 is b

It's quite logical and reasonably helpful sometimes* but the main reason you might see '0' is that it's easier to remember '0' than it is to remember 48 (it's slightly easier to remember the hex version 0x30)

All these give you char 5 from int 5:

char five = 5 + 48;
char five = 5 + 0x30;
char five = 5 + '0';

Which one would you find easiest to remember? :)


*for example, say you wanted to count the chars in an ascii string, you could do:

var counts = new int[256];
foreach(char c in string)
  counts[c]++;

You can use the char to index the array just like you can an int. At the end of the operation "hello world" would have put a 1 in index 104 (the h), a 3 in index 108(the l) etc..

Sure these days you might use a Dictionary<char, int> but appreciating that intrinsic char/int equivalence and how it can be used has its merits..

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
1

What is '0' means?

In C++, it is a character literal.

The fundamental reason why/how str1[i] - '0' works is through promotion.

In particular when you wrote:

str1[i]-'0'

This means

both str1[i] and '0' will be promoted to an int. And so the result will be an int.

Let's looks at some example for more clarifications:

char c1 = 'E';
char c2 = 'F';

int result = c1 + c2;

Here both c1 and c2 will be promoted to an int. And the result will be an int. In particular, c1 will become(promoted to) 69 and c2 will become(promoted to) 70. And so result will be 69 + 70 which is the integer value 139.

Jason
  • 36,170
  • 5
  • 26
  • 60