19

For example, here's a reference for fread:

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

Reads an array of count elements, each one with a size of "size bytes"... So how many BITS will read an fread(&x, 1, 1, stream)? Eight or CHAR_BIT?

Kos
  • 70,399
  • 25
  • 169
  • 233
user389419
  • 243
  • 1
  • 2
  • 8
  • 3
    That's from the Linux manpage, right? The C standard's definition doesn't mention bytes at all: "The `fread` function reads, into the array pointed to by `ptr`, up to `nmemb` elements whose size is specified by `size`, from the stream pointed to by `stream`." – Fred Foo Nov 28 '11 at 13:27
  • 6
    And given that it's from the Linux man page, `CHAR_BIT` is guaranteed (by Posix) to be equal to 8. – Steve Jessop Nov 28 '11 at 13:37
  • 1
    The C standard **does** mention bytes. Section 3.6 defines a byte as the smallest addressable unit. – JeremyP Nov 28 '11 at 14:00
  • 1
    @JeremyP: the C standard does. "The C standard's definition" (of fread) doesn't. – Steve Jessop Nov 28 '11 at 15:35
  • 1
    Of course, one should code defensively. While it's unlikely you'll ever encounter a modern system with a different `CHAR_BIT` value, it's good to be aware of the semantics here. – Jonathan Grynspan Nov 28 '11 at 15:57
  • 1
    @Jonathan: and even if you do encounter one, it's unlikely that you'd be using `fread` on it, because it's probably some DSP with a freestanding C implementation and no byte-based standard I/O. – Steve Jessop Nov 28 '11 at 16:36
  • @Steve Oh god, don't scare me like that. – Jonathan Grynspan Nov 28 '11 at 16:48
  • 8
    Additional $0.02: When you need an unambiguous term to refer to an 8-bit piece of meaningful data, call it an "octet". – Brian McFarland Nov 28 '11 at 16:54
  • @Jonathan: If you want scary... I've worked with legacy systems where addressable boundaries were not multiples of 8, but rather multiples of 12. These systems were built before widespread use of C. However, they had to talk to modern computers. What gets really fun is when you have 3 different ways to represent chars: a native (non-ascii) 6-bit format, 8-bit ASCII packed 3 chars / 24-bits, and 8-bit ASCII @ 1char / 12-bits. Thank god there was no unicode. – Brian McFarland Nov 28 '11 at 17:03
  • @Brian: Again with my scaring! (+1 for the "octet" comment of course.) – Jonathan Grynspan Nov 28 '11 at 17:09

2 Answers2

27

C99, §3.6:

byte

addressable unit of data storage large enough to hold any member of the basic character set of the execution environment

and §5.2.4.2.1:

CHAR_BIT — number of bits for smallest object that is not a bit-field (byte)

Thus, a "byte" contains CHAR_BIT bits.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
17

CHAR_BIT. The bit width of a byte is implementation-defined and is available to the developer via the CHAR_BIT macro.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104