4

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?

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Ananda
  • 1,572
  • 7
  • 27
  • 54
  • 1
    possible duplicate of [Can the char type be categorized as a integer?](http://stackoverflow.com/questions/11671908/can-the-char-type-be-categorized-as-a-integer) – pb2q Jul 28 '12 at 05:27

3 Answers3

12

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    when you said "If it's larger, it still occupies exactly one byte -- but that byte happens to be larger than 8 bits." what do you mean by "but that byte happens to be larger than 8 bits." – Vemuri Pavan Sep 17 '20 at 13:21
  • I'm confused about that, too. How can it be a byte but more than 8 bits? – Bored Comedy Apr 15 '21 at 17:09
  • 1
    @BoredComedy: A "byte" is normally the smallest unit of "storage" for a machine. Some DSPs, for example, store things in 16-bit or 24-bit chunks, and provide no way to work with 8-bit values at all--so on that hardware, a byte is 16 bits or 24 bits respectively. When they care specifically about an 8-bit unit (like a lot of networking standards and such) they normally use the word "octet", not "byte". – Jerry Coffin Apr 15 '21 at 17:29
5

The char type is an integral type in C, which is in the same family as other integral types such as short, int, long, etc.... Integral types can store whole integer values up to the number of encoding bits used to describe the integral type. For instance, on most platforms a char is eight-bits or a byte, and therefore it can represent up to 2^8 different values.

Jason
  • 31,834
  • 7
  • 59
  • 78
5

Yes, a char is (typically) a one-byte integer. Except the compiler knows to treat it differently, typically with ASCII character semantics. Many libraries / headers define a BYTE type that is nothing more than an unsigned char, for storing one-byte integers.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328