LESSONEmbedded C · Part 3EP.54 · 24 s

Data types on an 8-bit chip

// WHAT YOU WILL LEARN

uint8_t holds 0–255, uint16_t 0–65535; pick the smallest that fits; 255 + 1 wraps to 0.

// LESSON NOTES

Your chip counts to 255… and then it shows 0?! 🤯

1️⃣ ONE BYTE: uint8_t is 1 byte = 8 bits. 8 bits give 2⁸ = 256 values, so it holds 0 to 255.

2️⃣ NEED MORE? uint16_t is 2 bytes (16 bits): 0 to 65,535. int8_t is signed: bit 7 is the sign, so it holds −128 to 127.

3️⃣ OVERFLOW: a = 255; a = a + 1; → 11111111 + 1 needs a 9th bit… there isn't one, so a becomes 0. For unsigned types this wrap-around is normal, defined C.

📌 The ATmega328P on your Uno has only 2 KB of RAM: pick the smallest type that fits.

TESTED CODE · ec03_data_types.c · avr-gcc
// EC L03 - sizes matter on an 8-bit chip
#include <avr/io.h>
#include <stdint.h>

volatile uint8_t  a = 255;    // 1 byte : 0 .. 255
volatile uint16_t b = 65535;  // 2 bytes: 0 .. 65535
volatile int8_t   c = -128;   // 1 byte : -128 .. 127

int main(void) {
    a = a + 1;                // 255 + 1 wraps to 0
    while (1) { }
}
Read the voiceover (transcript)
  1. Your chip counts to two fifty-five... then zero?!
  2. You int eight is one byte: eight bits, zero to two fifty-five!
  3. Need more? Sixteen bits go up to sixty-five thousand. Signed int eight: minus one twenty-eight to one twenty-seven.
  4. Now add one to two fifty-five. It wraps to zero!
  5. Pick the smallest type that fits!
  6. Next: binary and hex! Follow for part four!

Related lessons