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';
Each box is one byte. The number above a box is its address, and the two characters inside are its value in hex. 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:
letterchar650x41

65 = 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).

Bytes used by each basic type on ARM32 and ARM64
Type ARM32bytes ARM64bytes
char11
short22
int44
long48differs
long long88
float44
double88
any pointer48differs

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 255
  • uint16_t: 16 bits, 2 bytes
  • uint32_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,896
Top: the number split into its 4 bytes, most significant on the left, the way you write it. Below: the same 4 bytes in memory, both ways. Each byte keeps its shade so you can follow where it went. The darkest one, 12, is the most significant.

Almost 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:

  1. Only the bytes swap, never the bits. Each byte keeps its own value: 78 is 0111 1000 in both orders.
  2. A single byte has no order. A char or uint8_t, and text, are stored exactly as written.
  3. 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};
In 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;
}
Edit the code and run it again. In the editor, Ctrl+Enter runs it (⌘+Enter on a Mac).

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)11
A char uses 1 byte: one box on each row. It holds a single character or a small number.

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,896
x needs 4 bytes. In hex every two digits are one byte, so 0x12345678 splits into 12, 34, 56 and 78. The 4 boxes below are empty memory, starting at address 0x2000_0000.

Try 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.

Type
uint32_t v0x1234_5678305,419,896

3Practice

How many bytes

Guess how many bytes each variable uses on each chip, then press Check.

  1. Q1 How many bytes does a use on each chip?

    int a;
  2. Q2 How many bytes does b use on each chip?

    long b;
  3. Q3 How many bytes does d use on each chip?

    double d;
  4. Q4 How many bytes does p use on each chip?

    int *p;
  5. Q5 How many bytes does v use on each chip?

    uint16_t v;
  6. Q6 How many bytes does values use on each chip?

    int values[5];

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.

  1. Q1 On an ARM chip, x is stored at address 0x2000_0000. Type the byte in each box.

    uint32_t x = 0x11223344;
  2. Q2 On ARM, port starts at 0x2000_0000. Type its 2 bytes.

    uint16_t port = 0x1F90;   /* 8080 */
  3. Q3 On ARM, n starts at 0x2000_0000. Type its 4 bytes. Hint: 1000 in hex is 0x3E8.

    uint32_t n = 1000;
  4. Q4 On ARM, b starts at 0x2000_0000. Type the byte in each box.

    uint8_t b[4] = {0x11, 0x22, 0x33, 0x44};
  5. Q5 The program sends x over a network, which uses big-endian order. Type the bytes in the order they go out.

    uint32_t x = 0x11223344;
  6. Q6 A debugger shows these 4 bytes from address 0x2000_0000 on an ARM chip. They hold one uint32_t. What is its value in hex?

    0x2000_000078
    0x2000_000156
    0x2000_000234
    0x2000_000312

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.

  1. C1 The sensor bug. A temperature sensor sends a 16-bit reading as two bytes, high byte first: first 0x01, then 0x2C. The ARM firmware stores them in memory in the order they arrive and reads them as one uint16_t.

    uint8_t  rx[2] = {0x01, 0x2C};   /* as received */
    uint16_t reading;
    memcpy(&reading, rx, 2);
    Show a hint

    Write down the two boxes in memory first. Which one does ARM treat as the least significant byte?

  2. C2 Arrays on two chips. How many bytes do these two arrays use together, on each chip?

    long  t[3];
    char *names[4];
    
    /* sizeof t + sizeof names = ? */
    Show a hint

    Two things change size between the chips. Both are in this code. A pointer's size does not depend on what it points to.

  3. C3 Which way round is this chip?. This short program is a common way to find out a chip’s byte order at run time.

    uint32_t x = 1;
    uint8_t  first;
    
    memcpy(&first, &x, 1);   /* copy only the first box of x */
    printf("%d\n", first);
    Show a hint

    Write 1 as a 4-byte number in hex first: 00 00 00 01. Then lay those bytes into memory both ways.

  4. C4 Reading from the middle. Memory on an ARM chip holds these 8 bytes. A program reads a uint32_t starting at address 0x2000_0002.

    0x2000_000011
    0x2000_000122
    0x2000_000233
    0x2000_000344
    0x2000_000455
    0x2000_000566
    0x2000_000677
    0x2000_000788
    Is 0x2000_0002 an aligned address for a uint32_t?
    Show a hint

    A uint32_t takes 4 boxes, starting at the one you are given. For alignment: is the address a multiple of 4?

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 char array; 6.2.8 alignment; 6.5.3.4 sizeof; 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.