daniyalahmad daniyalahmad - 2 months ago 7
C Question

Why char is of 1 byte in C language

Why is a

char
1
byte long in C? Why is it not
2
bytes or
4
bytes long?

What is the basic logic behind it to keep it as
1
byte? I know in Java a
char
is
2
bytes long. Same question for it.

Answer

char is 1 byte in C because it is specified so in standards.

The most probable logic is. the (binary) representation of a char (in standard character set) can fit into 1 byte. At the time of the primary development of C, the most commonly available standards were ASCII and EBCDIC which needed 7 and 8 bit encoding, respectively. So, 1 byte was sufficient to represent the whole character set.

OTOH, during the time Java came into picture, the concepts of extended charcater sets and unicode were present. So, to be future-proof and support extensibility, char was given 2 bytes, which is capable of handling extended character set values.

Comments