C Programming • C17 baseline, standards-specific features labeled
Object Representation, Alignment, and Endianness
Separate a value’s meaning from its in-memory byte representation.
Objects have representations made of bytes, and structures may contain padding. Alignment requirements vary by type and platform. Endianness describes byte order for multi-byte values, so raw structure bytes are not a portable file or network format.
Example
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int main(void) {
uint32_t value = 0x12345678u;
unsigned char bytes[sizeof value];
memcpy(bytes, &value, sizeof bytes);
printf("lowest-address byte: %02x\n", (unsigned)bytes[0]);
printf("object uses %zu bytes\n", sizeof value);
return 0;
}
What to expect
The number and order of bytes in the representation depend on the implementation.
Key idea: Serialize fields explicitly with a defined byte order; do not write raw structs as a portable interchange format.