I've always assumed:
- that a
char
is represented by a byte, - that a byte can always be counted upon to have 8 bits,
- that
sizeof (char)
is always1
, - and that the maximum theoretical amount of memory I can allocate (counted in
char
s) is the number of bytes of RAM (+ swap space).
But now that I've read the Wikipedia entry on the byte I'm not so sure anymore.
Which one(s) of my assumptions is wrong? Which one(s) is dangerous?
sizeof(char)
is defined to always be1
. From C99:It is not however guaranteed to be 8 bits. In practice, on the vast majority of platforms out there, it will be, but no, you cannot technically count on that to always be the case (nor should it matter as you should be using
sizeof
anyway).Yes,
char
andbyte
are pretty much the same. A byte is the smallest addressable amount of memory, and so is achar
in C.char
always has size 1.From the spec, section 3.6 byte:
And section 3.7.1 character:
A
char
hasCHAR_BIT
bits. It could be any number (well, 8 or greater according to the spec), but is definitely most often 8. There are real machines with 16- and 32-bitchar
types, though.CHAR_BIT
is defined inlimits.h
.From the spec, section 5.2.4.2.1 Sizes of integer types
<limits.h>
:sizeof(char) == 1
. Always.From the spec, section 6.5.3.4 The
sizeof
operator, paragraph 3:You can allocate as much memory as your system will let you allocate - there's nothing in the standard that defines how much that might be. You could imagine, for example, a computer with a cloud-storage backed memory allocation system - your allocatable memory might be practically infinite.
Here's the complete spec section 7.20.3.3 The
malloc
function:That's the entirety of the specification, so there's not really any limit you can rely on.
Concretely, some architectures, especially in the DSP field have char:s larger than 8 bits. In practice, they sacrifice memory space for speed.
But sizeof(char) is always 1.
Memorizing the C FAQ is a career-enhancing move.
sizeof(char)
is always 1 byte. A byte is not always one octet, however: The Texas Instruments TI C55x, for example, is a DSP with a 16-bit byte.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
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:Where,
type
is your data type. For example, to find out how many bits achar
takes up on your system, you can use the following:This is taken from the GNU C Library Reference Manual, which contains the following illuminating explanation on this topic: