Why does adding a '0' to an int digit allow conversion to a char?

后端 未结 6 1164
星月不相逢
星月不相逢 2020-12-31 11:28

I\'ve seen examples of this all over the place:

int i = 2;
char c = i + \'0\';
string s;
s += char(i + \'0\');

However, I have not yet seen

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 12:24

    A char will convert the integer to corresponding value based on ASCII value, So if you assign a value of 48 to a char it will store 0 since,48 is the ASCII value of 0.Correspondingly you add the ASCII value of 0 to any integer you need to convert to char

提交回复
热议问题