Embedded C Cheat Sheet

Fixed-width types, bit manipulation, volatile, register access, promotion pitfalls and interrupt-safe patterns

Most embedded C bugs come from a small set of language details: an int that is 16 bits on one target and 32 on another, a shift into the sign bit, a missing volatile, a read-modify-write that races an interrupt, or a cast that breaks alignment. This cheat sheet collects the idioms that avoid them on a few pages you can keep beside the keyboard.

Code is shown as plain text in the style of C99 and later. Register names and addresses are illustrative; use the vendor device header (for example a CMSIS header on Arm Cortex-M) in real code. The coding checklist reflects the ideas behind guidelines such as MISRA C at a concept level only; it does not reproduce or replace any guideline.

What is inside

  • Fixed-width and related types from stdint.h, stddef.h and stdbool.h
  • Bit manipulation idioms: set, clear, toggle, test, masks and bit fields
  • volatile and const: when to use each and how to read pointer declarations
  • Register access patterns, including write-1-to-clear and set/reset registers
  • Integer promotion, signedness and overflow pitfalls with examples
  • Interrupt-safe patterns: flags, critical sections and ring buffers
  • Common undefined behaviour and a coding-style checklist

Contents

  1. Fixed-width types
  2. Bit manipulation idioms
  3. volatile and const
  4. Register access patterns
  5. Integer promotion and overflow
  6. Interrupt-safe patterns
  7. Common undefined behaviour
  8. Coding-style checklist
  9. Notes and limits

Formats: PDF document. Sign in to download for free.

Related courses

Discuss your project

Email support@aavico.com or use Get in Touch when the website finishes loading.