The unfortunate thing (or maybe fortunate, depending on how you view things) is that the idea of what a byte is commonly thought as (8 bits) is not synonymous with what the C programming language considers a byte to be. Looking at some of the previous answers, a byte has an exact definition when it comes to the C programming language and nowhere in the definition does it mention a byte being 8 bits. It simply mentions that a byte is
"an addressable unit of data storage large enough to hold any member of
the basic character set of the execution environment."
So to answer your question of, “Will a char
always-always-always have 8 bits”, the answer is, not always, but most often it will. If you are interested in finding out just exactly how many bits of space your data types consume on your system, you can use the following line of code:
sizeof(type) * CHAR_BIT
Where, type
is your data type. For example, to find out how many bits a char
takes up on your system, you can use the following:
printf("The number of bits a 'char' has on my system: %zu\n", sizeof(char) * CHAR_BIT);
This is taken from the GNU C Library Reference Manual, which contains the following illuminating explanation on this topic:
There is no operator in the C language that can give you the number of
bits in an integer data type. But you can compute it from the macro
CHAR_BIT, defined in the header file limits.h. CHAR_BIT — This is the
number of bits in a char—eight, on most systems. The value has type
int. You can compute the number of bits in any data type type like
this:
`sizeof (type) * CHAR_BIT`
That expression includes padding bits as well as value and sign bits.