Home / Learn Chip Programming / Coding Standards: MISRA C, Naming and Safe Style

Coding Standards: MISRA C, Naming and Safe Style

Rules that keep C code safe in cars, medical devices and aircraft.

In shortRules that keep C code safe in cars, medical devices and aircraft.

Theory in plain words

Practical safe styleCompile with -Wall -Wextra -WerrorAlways use braces and a default caseNo magic numbers: #define or constOne return type, check every return valueAvoid recursion and dynamic memory in firmware
Practical safe style

What it is

MISRA C is a set of rules limiting risky C features. CERT C and the Barr group standard are similar. Teams also add naming and formatting rules.

Why we use it

Employers in automotive, medical and industrial work expect rules-based coding. Style rules prevent whole classes of bugs.

How it works

Use static analysis (cppcheck, clang-tidy, PC-lint) and compiler warnings. Review in pull requests. Document deviations.

Where it is used

  • Automotive ISO 26262, medical IEC 62304
  • Code reviews and interviews
  • Open source firmware quality

Spot the rule violations

Review this codeTry it Yourself

Static analysers find all of these automatically.

ToolUse
gcc -Wall -Wextra -Wconversionwarnings in every build
cppcheckfree static analysis
clang-tidystyle and bug checks
clang-formatautomatic formatting
PC-lint, Polyspace, Coveritycommercial MISRA checking

Quick quiz

1. A common MISRA rule about if statements is...

2. Which tool is free static analysis?