【发布时间】:2019-05-24 08:59:45
【问题描述】:
Kernighan 和 Ritchie 所著的C 编程语言一书,第二版在第 43 页关于类型转换一章中陈述:
char到int转换的另一个示例是函数lower,它将单个字符映射为小写 ASCII 字符集。如果字符不是大写字母,lower返回原样返回。/* lower: convert c to lower case; ASCII only */ int lower(int c) { if (c >= 'A' && c <= 'Z') return c + 'a' - 'A'; else return c; }
文本中没有明确提及,所以我想确保我理解正确:转换发生在您使用 char 类型的变量调用 lower 函数时,不是吗?尤其是表达式
c >= 'A'
与从int 到char 的转换无关,因为像'A' 这样的字符常量
从一开始就在内部作为int 处理,不是吗?编辑:或者这本书涵盖的 ANSI C 是不同的(例如,一个字符常量被视为char)?
【问题讨论】:
-
不,他们所说的转换发生在 inside 函数体中。
标签: c type-conversion kernighan-and-ritchie