是“char”一个特殊类型的整数变量? [英] Is "char" a special type of integer variable?

查看:445
本文介绍了是“char”一个特殊类型的整数变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到一个声明,说c中的char类型实际上是一个特殊形式
的整数 - 一个存储表示字符和符号的ASCII码数字。
这有效多远?这导致了另一个问题 - 在C中是否真的可以将char类型归类为整数?

I recently came across a statement saying that "char" type in c is really a special form of integer – one that stores the ASCII code numbers which represent characters and symbols. How far this is valid? This leads to another question that - Does really the char type can be categorized as an integer in C?

推荐答案

是的,在C , char 被认为是一个整数类型。它需要有至少8位。一个 char 和一个存储字节之间的等价关系是相当明确的,而不仅仅是通常发生的事情。例如,(C99,§5.2.4.2.1/ 1):

Yes, in C, char is considered an integer type. It's required to have a minimum of 8 bits. The equivalent between a char and a byte of storage is fairly explicit, not just something that usually happens. For example, (C99, §5.2.4.2.1/1):

number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT                                            8

所以,一个 char 总是占用一个字节,它必须至少为8位。如果它更大,它仍然占用一个字节 - 但该字节恰好大于8位。

So, a char always occupies exactly one byte, which must be a minimum of 8 bits. If it's larger, it still occupies exactly one byte -- but that byte happens to be larger than 8 bits.

就保存ASCII码而言,这通常是正确的,但不一定是这样。就像IBM大型机一样,它可能会保留EBCDIC代码。在更常见的机器上,ASCII或多或少会偶然发生,但编码非英文字符时,您很快就会发现它并不真正存储ASCII。它通常存储ISO 8859 / x或Unicode UTF-8。

As far as holding ASCII codes goes, that's frequently true, but not necessarily the case. On something like an IBM mainframe, it'll probably hold EBCDIC codes instead. On more common machines, "ASCII" happens more or less incidentally, but when encoding non-English characters, you'll quickly find that it's not really storing ASCII. It's typically storing ISO 8859/x, or perhaps Unicode UTF-8.

这篇关于是“char”一个特殊类型的整数变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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