C Reference ยท Types and Literals
Endianness in C
Concept page on byte order in C: little-endian versus big-endian, how to detect it safely, and how to read and write portable binary data with shifts.
A value wider than one byte must be stored as several bytes, and endianness says in which order. A little-endian machine stores the least significant byte at the lowest address; a big-endian machine stores the most significant byte first. The value 0x01020304 occupies the same four bytes either way, only their order differs.
Endianness never affects arithmetic or bitwise operators: x >> 8 always means "divide by 256" in value terms. It matters only when you look at an object's bytes (through unsigned char * or memcpy) or when you exchange binary data with files and networks. Almost every desktop, phone and server CPU today (x86, ARM in its usual mode, RISC-V) is little-endian; network protocols use big-endian, called network byte order.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
How to handle endianness
Never write raw structs or integers to a file or socket if the data must be read on another machine. Padding, sizes and byte order all vary. Instead, encode each field explicitly:
- Use fixed-width types from
<stdint.h>for the fields. - Build and take apart the bytes with shifts and masks, as
put_be32andget_be32do. This code is correct on any host, because shifts operate on values, not on memory. - Cast to the unsigned width before shifting (
(uint32_t)in[0] << 24), otherwise the byte promotes to a signedintand shifting into the sign bit is undefined behavior.
For network programming POSIX provides htons, htonl, ntohs and ntohl in <arpa/inet.h> ("host to network short/long"). They are no-ops on big-endian hosts. C23 adds <stdbit.h> with stdc_endian constants, but the runner on this site does not ship that header, so detect byte order at run time as shown above or with the compiler macro __BYTE_ORDER__ (GCC and Clang).
Usage notes
- Detection at run time: inspect the first byte of a known multi-byte value, via
memcpyorunsigned char *. Aunionread is also well defined in C (it is not in C++). - Compile-time detection with
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__works on GCC and Clang, but it is not standard. - Bit-fields and bit order are separate issues: the layout of bit-fields in a struct is implementation-defined.
- Floating-point values have byte order too, and also their own format. Serialize them as text, or
memcpythem into auint64_tfirst and then use integer code. - Text encodings: UTF-8 has no endianness; UTF-16 and UTF-32 do (and use a byte order mark).
- Hex dumps look reversed on little-endian hosts: the integer
0x01020304shows as04 03 02 01. - Performance: compilers recognize the shift pattern for loading a big-endian value and turn it into a single load plus a byte-swap instruction.