What is the meaning of this expression

I am trying to push a integer that is stored in a string to a stack of type int I was trying to do the same by using stack.push(str[i]) which resulted in some weird values in the final outcome

Github copilot suggeted to do it this way which was succesfull

else
       {
           temp.push(exp[i]-'0');
       }

what is the meaning of -‘0’ here

>Solution :

The char ‘0’ has ascii value 0x30 (48 decimal).

The char ‘9’ has ascii value 0x39 (57 decimal).

If you do '9' - '0' = 57 - 48 = 9.

So you are converting a digit from its ascii number '9' = 57 to its numeric value 9.

This is often used when fast-converting a string of integers to its numeric value.

Leave a Reply