
Byte order matters when working with binary protocols, file formats, embedded systems, and low-level debugging. Interviewers ask this question to test whether you understand how integers are stored in memory and how C exposes raw memory.
Explain how you would determine whether a machine is little-endian or big-endian in C.
Your answer should cover:
The interviewer is usually looking for a short low-level explanation plus a small C example. You should be able to describe the memory layout of a value like 0x00000001, explain the role of pointers or byte access, and mention that this technique detects the host machine's runtime representation rather than changing it.
Endianness is the order in which a multi-byte value is stored in memory. In little-endian systems, the least significant byte comes first; in big-endian systems, the most significant byte comes first.
unsigned int x = 1; /* 0x00000001 */
C allows examining an object's memory through a character pointer. By reading the first byte of an integer, you can infer whether the least significant byte is stored at the lowest memory address.
unsigned char *p = (unsigned char *)&x;
Using the value 1 is convenient because its binary representation has only the least significant byte set. That makes the first byte easy to interpret: 1 means little-endian, 0 means big-endian for a typical multi-byte integer.
if (p[0] == 1) printf("Little-endian
");
The C standard permits accessing any object's byte representation through char * or unsigned char *. This is why the technique is valid for inspecting raw bytes without violating aliasing rules.
unsigned char first = *((unsigned char *)&x);
This method reports the machine's runtime byte order for the current platform and compiler target. It does not address network byte order conversions, mixed-endian architectures, or serialization requirements across systems.