What is a "byte" in C / C++
Asked Answered
V

2

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?

Valida answered 28/11, 2011 at 13:24 Comment(10)
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."Vagrancy
And given that it's from the Linux man page, CHAR_BIT is guaranteed (by Posix) to be equal to 8.Tillett
The C standard does mention bytes. Section 3.6 defines a byte as the smallest addressable unit.Pack
@JeremyP: the C standard does. "The C standard's definition" (of fread) doesn't.Tillett
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.Millar
@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.Tillett
@Steve Oh god, don't scare me like that.Millar
Additional $0.02: When you need an unambiguous term to refer to an 8-bit piece of meaningful data, call it an "octet".Bethesde
@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.Bethesde
@Brian: Again with my scaring! (+1 for the "octet" comment of course.)Millar
A
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.

Antarctica answered 28/11, 2011 at 13:30 Comment(0)
M
17

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

Millar answered 28/11, 2011 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.