从 C 中的变量中减去“0"是什么意思? [英] What does it mean to subtract '0' from a variable in C?

查看:33
本文介绍了从 C 中的变量中减去“0"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void push(float[],float);

这里,st[]是浮点数据类型栈,exp[]是存储后缀表达式的char数据类型数组.

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

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

我无法弄清楚 (exp[i]-'0') 部分的用途.为什么我们要减去'0'?

I couldn't figure out the purpose of (exp[i]-'0') section though. Why are we subtracting '0'?

推荐答案

一个字符基本上无非是一个整数,其值就是字符的编码.

A character is basically nothing more than an integer, whose value is the encoding of the character.

在最常见的编码方案中,ASCII,例如的值字符 '0'48,例如值'3'51.现在,如果我们有一个包含字符 '3' 的变量 someChar 并且您执行 someChar - '0' 它与执行 相同>51 - 48 这将导致值 3.

In the most common encoding scheme, ASCII, the value for e.g. the character '0' is 48, and the value for e.g. '3' is 51. Now, if we have a variable someChar containing the character '3' and you do someChar - '0' it's the same as doing 51 - 48 which will result in the value 3.

因此,如果您将某个数字从某处读取为字符,那么您可以减去 '0' 以获得该数字的整数值.

So if you have a digit read as a character from somewhere, then you subtract '0' to get the integer value of that digit.

这也适用于其他编码,不仅仅是 ASCII,因为 C 规范规定所有编码必须具有连续顺序的数字.

This also works on other encodings, not only ASCII, because the C specification says that all encodings must have the digits in consecutive order.

注意这个技巧"保证适用于任何非数字字符.

Note that this "trick" is not guaranteed to work for any non-digit character.

这篇关于从 C 中的变量中减去“0"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆