Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 953
daniyalahmad Avatar asked May 11 '15 11:05

daniyalahmad


People also ask

Is a char always 1 byte in C?

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.

Is a char 1 byte?

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.

Can char be 2 bytes in C?

Even if you think of a "character" as a multi-byte thingy, char is not. sizeof(char) is always exactly 1. No exceptions, ever.

Why is a char 2 bytes?

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.


2 Answers

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.

like image 132
Sourav Ghosh Avatar answered Sep 20 '22 15:09

Sourav Ghosh


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.

like image 22
Nidhoegger Avatar answered Sep 20 '22 15:09

Nidhoegger