What is MISRA C?
Short answer: MISRA C is a set of coding guidelines for writing safe, secure, portable and reliable C in critical systems. It was created for the automotive industry by the Motor Industry Software Reliability Association, and is now used in aerospace, medical, railway and industrial devices. Rules restrict language features that commonly cause undefined behaviour or bugs, such as unchecked dynamic memory, recursion, unrestricted goto, implicit conversions between "essential types", and unions used for type punning. Compliance is checked with static analysis tools.
Structure
- Directives (process-level, partly not checkable by tools, e.g. "all code shall be traceable to requirements") and rules (checkable on the source).
- Each is Mandatory (no deviations allowed), Required (deviations need a documented, approved justification), or Advisory.
- Decidable vs undecidable rules: some can always be checked by a tool, others only partly.
Example rules (paraphrased)
- No dynamic memory allocation from the heap (Rule 21.3).
- Functions shall not call themselves, directly or indirectly (Rule 17.2).
- The value of an expression shall not be assigned to an object with a narrower essential type (Rule 10.3).
- A
switchstatement shall have adefaultlabel (Rule 16.4). - The return value of a non-void function shall be used (Rule 17.7).
- No implicit conversion of a signed value to unsigned in arithmetic (essential type model).
- Octal constants shall not be used (
010is 8, a common mistake).
Editions: MISRA C:1998, :2004, :2012 (with amendments adding security rules and C11 support), and the consolidated MISRA C:2023 (adds C11/C18 coverage, including threads and atomics). MISRA C++:2023 covers modern C++.
Tools: PC-lint Plus, Polyspace, Coverity, Parasoft C/C++test, Helix QAC, LDRA, Klocwork, cppcheck's MISRA addon (partial), and compiler-integrated checkers (IAR C-STAT, Arm, Green Hills).
How it is used in practice: projects agree a "compliance plan" (which rules apply), integrate the checker into CI, document deviations with justifications, and record compliance evidence for standards such as ISO 26262 (automotive), IEC 61508 (industrial), IEC 62304 (medical) and DO-178C (avionics). Related standards: CERT C (security-focused) and AUTOSAR C++14 guidelines.