C basics · 1 of 8
Types, sizes and byte order
How many bytes each type uses on 32-bit and 64-bit ARM, how a number is laid out in those bytes, and how to pick a type with an exact size.
1Read
Memory is a row of bytes
Think of memory as a long row of boxes. Each box is one byte and has its own number, called its address. On ARM a byte is 8 bits, so it can hold a number from 0 to 255. A variable uses one or more boxes next to each other.
Bytes are usually written in hex (base 16): two hex digits per byte, from 00 to FF, which is 0 to 255. A 0x in front of a number means it is hex.
int n = 1000;
char letter = 'A';n holds 1000, which is 3E8 in hex and needs more than one byte, so it takes four. (Why they read E8 03 00 00 and not 00 00 03 E8 is explained under byte order below.) letter fits in one byte: the character A is stored as the number 65, which is 41 in hex. Inside that byte are 8 bits:letterchar650x4165 = hex 41 = binary 0100 0001.
sizeof: how many bytes
sizeof tells you how many bytes a variable or a type uses. sizeof(int) is 4 on ARM.
The size decides how big a number fits:
- 1 byte: 256 different values
- 2 bytes: 65,536 values
- 4 bytes: about 4.3 billion values
You also need it whenever you copy, save or send data, to know how many bytes to move.
Sizes depend on the chip
C does not fix most sizes. It only sets a minimum, for example an int must have at least 16 bits. Each chip family picks the real sizes, and every compiler for that chip follows them.
32-bit ARM (ARM32) and 64-bit ARM (ARM64) agree on almost every type. They differ on two: long, and pointers (a pointer stores an address).
| Type | ARM32bytes | ARM64bytes |
|---|---|---|
char | 1 | 1 |
short | 2 | 2 |
int | 4 | 4 |
long | 4 | 8differs |
long long | 8 | 8 |
float | 4 | 4 |
double | 8 | 8 |
any pointer | 4 | 8differs |
When the size must not change: <stdint.h>
Because long changes size between chips, embedded code uses types with the size in their name. They come from <stdint.h>:
uint8_t: 8 bits, 1 byte, 0 to 255uint16_t: 16 bits, 2 bytesuint32_t: 32 bits, 4 bytes
The u means the number is never negative. Drop it (int8_t, int16_t, int32_t) when you need negative numbers. These sizes are the same on every chip.
#include <stdint.h>
uint8_t small = 200; /* 1 byte: 0 to 255 */
uint16_t medium = 50000; /* 2 bytes: 0 to 65,535 */
uint32_t big = 70000; /* 4 bytes: 0 to about 4.3 billion */
How a number is stored: byte order
A uint32_t uses 4 boxes. Take uint32_t x = 0x12345678;. In hex every two digits make exactly one byte, so the number splits into four bytes: 12, 34, 56 and 78. That is the main reason embedded code writes values in hex.
12 is the most significant byte: like the 1 in 1,000, it counts the most. 78 is the least significant byte: it counts the least.
So which byte goes into the first box, the one with the lowest address? Chips do it one of two ways. The choice is called the byte order, or endianness:
- Little-endian: least significant byte first. Memory reads 78 56 34 12.
- Big-endian: most significant byte first. Memory reads 12 34 56 78, the same order you write the number.
uint32_t x = 0x12345678;uint32_t x0x1234_5678305,419,896Almost every ARM chip you will meet runs little-endian: Cortex-M microcontrollers, and 64-bit ARM in phones and the Raspberry Pi. ARM chips can be built or set up to run big-endian, but that is rare. PCs (x86) are little-endian too.
Three things that trip people up:
- Only the bytes swap, never the bits. Each byte keeps its own value: 78 is 0111 1000 in both orders.
- A single byte has no order. A
charoruint8_t, and text, are stored exactly as written. - Arrays keep their order. Item [0] is always at the lowest address, then item [1], and so on. Byte order only applies inside each item.
uint32_t w[2] = {0x11223344, 0x55667788};
uint8_t b[4] = {0x11, 0x22, 0x33, 0x44};w, item [0] comes first and item [1] after it, as written. Only the 4 bytes inside each item are reversed. In b every item is a single byte, so nothing is reversed: memory reads 11 22 33 44, exactly as written. The two arrays are drawn at separate addresses here; where the compiler really puts them is a later lesson.When byte order matters
Inside one program you rarely notice it. When the program reads x, the chip puts the 4 bytes back together and gets 0x12345678. Byte order matters when bytes are handled one at a time:
- Sending data over a network or a serial link, or saving it to a file. Both sides must agree on the order. Internet protocols send the most significant byte first (big-endian), so a little-endian ARM chip has to swap the bytes before sending.
- Reading a sensor. Many sensor chips send the high byte first, and the firmware puts the value back together.
- Reading memory in a debugger. A memory view shows
78 56 34 12, and you read it from the last box back to get 0x12345678.
To see the bytes yourself, copy a number into an array of bytes and print them. Press Run. Then change 0x12345678 to another number, like 1000, and run it again: you should get E8 03 00 00.
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
uint32_t value = 0x12345678;
uint8_t bytes[4];
memcpy(bytes, &value, 4); /* copy the 4 bytes of value, first box first */
printf("%02X %02X %02X %02X\n", bytes[0], bytes[1], bytes[2], bytes[3]);
/* prints on ARM and on a PC: 78 56 34 12 */
return 0;
}
Your browser runs it as WebAssembly, which stores bytes little-endian like ARM, so the output is the same as on an ARM chip. It runs in your browser with picoc, a small C interpreter. Most beginner C works; long long and 64-bit types do not yet.
&value means "the address of value" (addresses get their own lesson), and %02X prints a byte as two hex digits.
Alignment: where a variable may start
On ARM, each basic type must start at an address that is a multiple of its size. A 4-byte int can start at address 0x2000_0000 or 0x2000_0004, but not at 0x2000_0001. This is called alignment.
The chip reads memory fastest this way, and some small ARM chips stop with an error if the rule is broken. You rarely have to think about it, because the compiler places every variable correctly for you.
2Watch
How big each type is
Each step shows one variable twice: the top row is 32-bit ARM, the bottom row is 64-bit ARM. Use the arrows to step through, or press play.
char c = 'A';char c = 'A';1 of 8
- One box is one byte (8 bits). A filled box is a byte the variable uses.
- Dashed: memory after the variable, not part of it.
- Byte numbers: +0 is the variable's first byte, +1 the next, and so on.
| Property | ARM32 | ARM64 |
|---|---|---|
sizeof(c) | 1 | 1 |
How a number goes into memory
Watch x = 0x12345678 go into memory one byte at a time, first little-endian, then big-endian.
uint32_t x = 0x12345678;Split into bytes1 of 9
uint32_t x0x1234_5678305,419,896Try your own number
Type any number, in decimal or in hex with 0x in front, and pick a type. Each byte shows its 8 bits under it.
uint32_t v0x1234_5678305,419,8963Practice
How many bytes
Guess how many bytes each variable uses on each chip, then press Check.
Which byte goes where
Type each byte as two hex digits, like 0A or FF. The boxes move to the next one as you type.
Challenge
Four harder ones that mix everything in this chapter. Questions like these come up in embedded interviews. Each has a hint if you get stuck.
Sources
- C standard (C11, draft N1570): 5.2.4.2.1 minimum sizes of integer types; 6.2.5p20 array items are stored one after another; 6.2.6.1p2 the order of a value's bytes is up to the implementation; 6.2.6.1p4 copying a value's bytes into an
unsigned chararray; 6.2.8 alignment; 6.5.3.4sizeof; 6.5.6p8 item order in an array; 7.20.1<stdint.h>types. - Arm's rules for C on 32-bit ARM (AAPCS32) and 64-bit ARM (AAPCS64): sizes and alignment of the basic types.
- Arm, ARMv6-M and ARMv7-M Architecture Reference Manuals: A3.2 alignment, A3.3 endian support.
- RFC 791, Appendix B: Internet data is sent most significant byte first.
- WebAssembly Core Specification, memory instructions: loads and stores use little-endian byte order.
- Checked with GCC 13.3: the sizes against 64-bit ARM; the byte orders by compiling the examples for 64-bit ARM in little-endian and big-endian mode and dumping the stored bytes; the example programs compiled and run.