stack - What does it mean to subtract '0' from a variable in C? -


void push(float[],float); 

here, st[] float data-type stack , exp[] char data-type array storing postfix expression.

  push(st,(float)(exp[i]-'0')); 

i couldn't figure out purpose of (exp[i]-'0') section though. why subtracting '0'?

a character nothing more integer, value encoding of character.

in common encoding scheme, ascii, value e.g. character '0' 48, , value e.g. '3' 51. now, if have variable somechar containing character '3' , somechar - '0' it's same doing 51 - 48 result in value 3.

so if have digit read character somewhere, subtract '0' integer value of digit.

this works on other encodings, not ascii, because c specification says encodings must have digits in consecutive order.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -