Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c - Is "char" a special type of integer variable?

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 is this valid? This leads to another question that - Can the char type really be categorized as an integer in C?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...