{"docs":[{"id":144,"title":"Coding Standards for Embedded C: MISRA and Defensive Practice","heading":"Coding Standards for Embedded C: MISRA and Defensive Practice","subheading":null,"category":"Embedded Software","body":{"root":{"type":"root","children":[{"type":"heading","tag":"h3","version":1,"children":[{"type":"text","text":"Defensive habits beyond the rules","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]}],"direction":"ltr","format":"","indent":0,"version":1}},"tags":"Embedded Software, MISRA C, Code Quality","cardCta":{"label":null,"link":null},"pageCta":{"label":"Talk to AAVICO engineering","link":"/contact"},"excerpt":"C gives embedded engineers control and many ways to write undefined behaviour. A coding standard, enforced by tools, removes the riskiest constructs before they reach a product.","featuredImage":null,"type":"Blog","readTime":"3 min read","publishedAt":"2026-11-15T20:19:00.000Z","heroImage":null,"introText":"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.","metrics":[{"id":"coding-standards-for-embedded-c-misra-and-defens-metric-1","value":"MISRA C","label":"Guidelines for a safer subset of C, first published in 1998 for the motor industry.","icon":null},{"id":"coding-standards-for-embedded-c-misra-and-defens-metric-2","value":"3 categories","label":"Guidelines are mandatory, required or advisory, which sets how strictly each applies.","icon":null},{"id":"coding-standards-for-embedded-c-misra-and-defens-metric-3","value":"Deviations","label":"Documented, justified exceptions are part of compliance, not a failure of it.","icon":null},{"id":"coding-standards-for-embedded-c-misra-and-defens-metric-4","value":"Tools","label":"Static analysers check most guidelines automatically; review covers the rest.","icon":null}],"gallery":[],"challenge":{"root":{"type":"root","children":[{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]}],"direction":"ltr","format":"","indent":0,"version":1}},"blockquote":{"quote":"A coding standard is only as good as the tool that enforces it and the process that handles its exceptions.","author":"","role":"AAVICO Engineering"},"supportImage1":null,"deliverables":{"root":{"type":"root","children":[{"type":"paragraph","version":1,"children":[{"type":"text","text":"Choose a standard and a specific edition, such as MISRA C:2012 or MISRA C:2023, and record which guidelines the project applies.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"Configure a static analyser to check them in every build, and record a baseline so existing code does not block new work.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"Require new and changed code to be clean, and handle every exception through a written deviation with a reason and an approver.","version":1}]}],"direction":"ltr","format":"","indent":0,"version":1}},"results":[{"id":"coding-standards-for-embedded-c-misra-and-defens-result-1","title":"Defined Behaviour","description":"Code avoids constructs whose result depends on the compiler or optimisation level."},{"id":"coding-standards-for-embedded-c-misra-and-defens-result-2","title":"Portability","description":"Firmware behaves the same when the toolchain or processor changes."},{"id":"coding-standards-for-embedded-c-misra-and-defens-result-3","title":"Review Focus","description":"Tools catch mechanical issues, so reviewers can concentrate on design and logic."},{"id":"coding-standards-for-embedded-c-misra-and-defens-result-4","title":"Customer Evidence","description":"A compliance record answers the questions safety-related customers ask."}],"lookingAhead":{"root":{"type":"root","children":[{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"Consider the SEI CERT C Coding Standard alongside MISRA where security matters; the two overlap but emphasise different risks.","version":1}]}],"direction":"ltr","format":"","indent":0,"version":1}},"supportImage2":null,"testimonial":{"quote":null,"author":null,"role":null,"photo":null},"challengesSolved":[{"id":"coding-standards-for-embedded-c-misra-and-defens-challenge-1","title":"Code that changes behaviour with optimisation level","description":null,"icon":null},{"id":"coding-standards-for-embedded-c-misra-and-defens-challenge-2","title":"Thousands of findings nobody reads","description":null,"icon":null},{"id":"coding-standards-for-embedded-c-misra-and-defens-challenge-3","title":"Rule exceptions with no recorded reason","description":null,"icon":null},{"id":"coding-standards-for-embedded-c-misra-and-defens-challenge-4","title":"Interrupt-shared data treated as atomic","description":null,"icon":null}],"relatedPosts":[],"contactUs":{"show":true},"updatedAt":"2026-11-15T17:32:32.682Z","createdAt":"2026-11-15T17:32:32.682Z"}],"hasNextPage":false,"hasPrevPage":false,"limit":10,"nextPage":null,"page":1,"pagingCounter":1,"prevPage":null,"totalDocs":1,"totalPages":1}