C字符值算术 [英] C character values arithmetic

查看:106
本文介绍了C字符值算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读《 The C Programming Language》一书中学习C的内容,偶然发现了算术s[i] - '0',他们说算术s[i] - '0'给出了s [i]中存储的字符的数值.我不太了解,如何通过减法给出价值? 注意这在atoi函数中使用,该函数将一串数字转换为等效的数字. 谢谢

I have been reading from the book "The C Programming Language" learning C, and I stumbled upon the arithmetic s[i] - '0' which they said that it gives the numeric value of the character stored in s[i]. I didn't quite understand it, how could it give the value by subtraction? Note This is used in the atoi function, which converts a string of digits into its numeric equivalent. Thanks

推荐答案

基本上,您需要了解的是,在现代计算机上,所有信息都以数字形式存储为字节序列.由每个程序决定如何解释每个字节.因此,字符不过是一个或多个字节-一个通常代表人类可读字母或符号的数字值.例如,在ASCII中,字母"A"由数值65表示.

Basically, what you need to understand is that on a modern computer, all information is stored digitally as a sequence of bytes. It's up to each program to decide how to interpret each byte. So a character is nothing but one or more bytes - a numerical value which usually represents a human-readable letter or symbol. For example, in ASCII, the letter 'A' is represented by the numerical value 65.

这意味着在某些编程语言(例如C)中,您可以将字符视为数字值.例如,在C语言中,表达式'A' + 1将为您提供66,这恰好是B的ASCII值(假设您的编译器/平台使用的是ASCII兼容字符集).

What this means is that in certain programming languages, such as C, you can treat characters as numerical values. For example, in C the expression 'A' + 1 would give you 66, which happens to be the ASCII value for B (assuming your compiler/platform is using an ASCII-compatible character set).

这篇关于C字符值算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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