liveBook · Manning

Bytes

As explained in Modern C, Third Edition

A byte is the C memory unit corresponding to the storage occupied by one char object and used as the basic unit of object representation.

A byte is the C memory unit used to account for object storage. Each object consists of bytes, and sizeof measures how many bytes it occupies. The character types char, signed char, and unsigned char each occupy exactly one byte; use char for character data and unsigned char as the atom of object representations. section 12.1

Key rules:

  • sizeof(char) is 1 by definition, while sizeof(T) gives the size of an object or type T in bytes. section 12.1
  • Every object can be viewed as an array of unsigned char with length sizeof(object). section 12.1
  • Pointers to character types receive special treatment when accessing object representations. section 12.1

Inspecting an unsigned value

A union can overlay an unsigned value with an unsigned-char array: assign 0xAABBCCDD to val, then print bytes from bytes[0] onward. One observed output is DD, CC, BB, AA. This reveals the machine's byte order, but the order is implementation-defined rather than standardized. section 12.2

typedef union unsignedInspect unsignedInspect;
union unsignedInspect {
  unsigned val;
  unsigned char bytes[sizeof(unsigned)];
};
unsignedInspect twofold = { .val = 0xAABBCCDD, };

Shows a union whose unsigned value and unsigned-char array occupy the same storage, making the object representation inspectable. section 12.2

Dynamically allocated storage initially appears only as a byte array without an object interpretation; storing a value gives that storage a type. section 13.0

How Modern C, Third Edition covers it

How deep it goes
Defines byte size and object representation, then works through union-based inspection and implementation-defined byte order.
Examples
Uses an unsigned value 0xAABBCCDD, an unsigned-char union member, and printed byte values to demonstrate representation order.
What it assumes
Assumes familiarity with C objects, types, arrays, unions, and sizeof while defining the byte model incrementally.
What you can do after
Evaluate object representations with sizeof and inspect individual bytes through an unsigned-char overlay.
How it connects
Connects bytes with sizeof, object representation, character pointers, unions, endianness, and dynamic allocation.

Also explained in

Compare how 7 books explain bytes →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.