Answer: sizeof
returns the size of the type in bytes.
Example: sizeof(char)
is 100% guaranteed to be 1
, but this does not mean, that it's one octet (8 bits).
Proved by the standard:
in 6.5.3.4, point 2:
The sizeof operator yields the size (in bytes) of its operand, which may be an
expression or the parenthesized name of a type. The size is determined from the type of
the operand. The result is an integer. If the type of the operand is a variable length array
type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an
integer constant.
...
When applied to an operand that has type char, unsigned char, or signed char,
(or a qualified version thereof) the result is 1. When applied to an operand that has array
type, the result is the total number of bytes in the array) When applied to an operand
that has structure or union type, the result is the total number of bytes in such an object,
including internal and trailing padding.
Also, in Section 3.6, point 3:
A byte is composed of a contiguous sequence of bits, the number of which is implementation-defined
char
) hasCHAR_BITS
bits. If you want the number of octets in aT
, dosizeof(T) * CHAR_BITS / 8
. On most platforms,CHAR_BITS
is8
.CHAR_BIT
(there's no S) :)