Coding Standards for Embedded C: MISRA and Defensive Practice

C remains the main language of embedded firmware, and it trusts the programmer completely. Signed overflow, out-of-bounds access and implicit conversions compile without complaint and can behave differently between compilers and optimisation levels. A coding standard restricts the language to a subset whose behaviour is defined and predictable. MISRA C is the best known for embedded and safety-related work. This article explains how it is structured and how to adopt it sensibly.

At a glance

MISRA C
Guidelines for a safer subset of C, first published in 1998 for the motor industry.
3 categories
Guidelines are mandatory, required or advisory, which sets how strictly each applies.
Deviations
Documented, justified exceptions are part of compliance, not a failure of it.
Tools
Static analysers check most guidelines automatically; review covers the rest.

The problem

Undefined behaviour is the core risk. Code that relies on it may work with one compiler and fail with the next, or change when optimisation is turned up for production. These faults rarely show in testing because they depend on the build, not the input.

Standards also fail when they are adopted badly. Applying every rule to a large existing codebase at once produces thousands of findings, and teams either stop reading the reports or make hurried cosmetic changes that add risk.

A coding standard is only as good as the tool that enforces it and the process that handles its exceptions.

Defensive habits beyond the rules

Several practices sit naturally alongside a formal standard. Use the fixed-width types from stdint.h so integer sizes do not change between targets. Declare data shared with interrupts as volatile, remembering that volatile prevents caching but does not make an access atomic. Check every return value, and avoid dynamic memory allocation after start-up to rule out heap fragmentation.

Turn compiler warnings up to a high level and treat them as errors in the release build. Warnings are the cheapest static analysis available, and they catch many of the same problems.

What good looks like

Choose a standard and a specific edition, such as MISRA C:2012 or MISRA C:2023, and record which guidelines the project applies.

Configure a static analyser to check them in every build, and record a baseline so existing code does not block new work.

Require new and changed code to be clean, and handle every exception through a written deviation with a reason and an approver.

MISRA C guideline categories
CategoryMeaningExceptions
MandatoryMust always be followedNo deviation permitted
RequiredMust be followed unless formally deviatedDocumented deviation with justification
AdvisoryRecommended good practiceMay be disapplied, with the decision recorded
Summarised from MISRA C:2012 and MISRA Compliance:2020. Refer to the documents for the exact terms.

Adopting a standard on existing code

  1. Choose: Standard, edition and scope
  2. Configure: Static analyser in the build
  3. Baseline: Record existing findings
  4. Enforce: New and changed code is clean
  5. Deviate: Written reason for each exception
Baseline first, then hold new work to the standard.

What it changes

Defined Behaviour

Code avoids constructs whose result depends on the compiler or optimisation level.

Portability

Firmware behaves the same when the toolchain or processor changes.

Review Focus

Tools catch mechanical issues, so reviewers can concentrate on design and logic.

Customer Evidence

A compliance record answers the questions safety-related customers ask.

Where to go next

If the product falls under a functional safety standard such as IEC 61508 or ISO 26262, check what it expects of coding rules before settling on your own.

Consider the SEI CERT C Coding Standard alongside MISRA where security matters; the two overlap but emphasise different risks.

Problems this prevents

  • Code that changes behaviour with optimisation level
  • Thousands of findings nobody reads
  • Rule exceptions with no recorded reason
  • Interrupt-shared data treated as atomic

Discuss your project

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