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.
The size of both unsigned and signed char is 1 byte always, irrespective of what compiler we use. Here, a signed character is capable of holding negative values. Thus, the defined range here is -128 to +127.
The char type takes 1 byte of memory (8 bits) and allows expressing in the binary notation 2^8=256 values. The char type can contain both positive and negative values. The range of values is from -128 to 127.
Even if you think of a "character" as a multi-byte thingy, char is not. sizeof(char) is always exactly 1. No exceptions, ever.
And, every char is made up of 2 bytes because Java internally uses UTF-16. For instance, if a String contains a word in the English language, the leading 8 bits will all be 0 for every char, as an ASCII character can be represented using a single byte.
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.
Why would a char
hold more than 1byte? A char normally represents an ASCII character. Just have a look at an ASCII table, there are only 256 characters in the (extended) ASCII Code. So you need only to represent numbers from 0 to 255, which comes down to 8bit = 1byte.
Have a look at an ASCII Table, e.g. here: http://www.asciitable.com/
Thats for C. When Java was designed they anticipated that in the future it would be enough for any character (also Unicode) to be held in 16bits = 2bytes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With