Lesson 24 of 29 ↓
- 1.Rhel 9 Administration Guide
- 2.Ubuntu Boot Process Guide
- 3.Linux Basic Commands Guide
- 4.Comprehensive Unix Shells Guide
- 5.Mastering Vim Deep & Practical Guide
- 6.Magic Numbers in Linux Deep Systems Guide
- 7.Wildcards & Globbing in Linux Deep Cli Guide
- 8.Linux Users & Groups Deep Administration Guide
- 9.Journaling in Linux Filesystems Deep Systems Guide
- 10.Getopt & Getoptlong Deep System-level Option Parsing Guide
- 11.Su Command in Linux Complete Guide with Environment Variables
- 12.Linux File Permissions and Ownership Complete Practical Guide
- 13.Ubuntu Package Management Apt Complete Guide
- 14.Bash Scripting Basics
- 15.Linux File System Essentials
- 16.Process and Thread Management
- 17.Session 10 : Introduction to Networking & the Application Layer
- 18.Linux Networking Cli & Introduction to Yocto for Machine Routing
- 19.Comprehensive Guide to Ssh (secure Shell)
- 20.Logical Volume Management (lvm) Guide
- 21.C Build Process & Libraries Guide
- 22.Archiving and Compression in Linux
- 23.Linux Package Managers: a Detailed Guide
- 24.Misra-c Guidelines Detailed Cheat Sheet
- 25.Mqtt Server Setup (pc as Server)
- 26.Detailed Explanation of Tty and Pts in Linux
- 27.Vim Cheat Sheet
- 28.Difference Between apt and apt-get
- 29.Difference Between apt Upgrade and apt Full-upgrade
4 min read
Misra-c Guidelines Detailed Cheat Sheet
Target audience: Embedded Systems & Safety-Critical Software Developers Language: C (especially Bare‑Metal / MCU development)
What is Misra-c?
MISRA-C (Motor Industry Software Reliability Association) is a set of coding guidelines designed to:
-
Improve code safety and reliability
-
Avoid undefined / unspecified / implementation-defined behavior in C
-
Make code predictable and analyzable
-
Reduce runtime failures in embedded and safety‑critical systems
MISRA-C is widely used in:
-
Automotive (AUTOSAR)
-
Medical devices
-
Aerospace
-
Industrial controllers
Common Misra-c Versions
-
MISRA-C:2004
-
MISRA-C:2012 (most used in industry)
-
MISRA-C:2023 (latest)
Core Misra-c Rules (most Famous & Practical)
No Implicit Type Conversion
❌ Not recommended:
uint8_t x = 300;✅ Recommended:
uint8_t x = (uint8_t)300;Reason:
-
Prevent overflow
-
Prevent hidden truncation
-
Make programmer intent explicit
Use Fixed-width Integer Types
❌ Not recommended:
int x;✅ Recommended:
uint32_t x;Reason:
-
intsize is compiler-dependent -
Embedded systems require deterministic size
All Variables Must be Initialized
❌
int x;
if (x > 5)✅
int x = 0;Reason:
- Uninitialized variables cause undefined behavior
No Use of Undefined Behavior
Examples of forbidden behavior:
int x = 10 / 0; // division by zero
*p = 5; // NULL pointer dereference
x = i++ + i++; // multiple side effectsReason:
- Compiler may generate unpredictable code
Avoid Multiple Side Effects in One Expression
❌
a = i++ + i++;✅
i++;
a = i;
i++;
a += i;Always Use Braces {}
❌
if (x > 0)
y++;
z++;✅
if (x > 0)
{
y++;
}Reason:
- Prevent logical errors
Every if Must Have an else (recommended)
if (status == OK)
{
handle_ok();
}
else
{
/* explicitly handled */
}Every switch Must Have a default
switch (state)
{
case INIT:
break;
case RUN:
break;
default:
break;
}Reason:
- Protect against unexpected values
No goto Statement
❌
goto error;Reason:
- Breaks control flow readability
(Allowed only with strong justification)
No Dynamic Memory Allocation
❌
malloc();
free();✅
static uint8_t buffer[128];Reason:
-
Fragmentation
-
Non-deterministic timing
No Recursion
❌
void func(void)
{
func();
}Reason:
-
Stack overflow risk
-
No predictability
One Return Statement Per Function
❌
if (x) return 1;
return 0;✅
int ret = 0;
if (x)
{
ret = 1;
}
return ret;Function Prototypes are Mandatory
❌
func();✅
void func(void);No Magic Numbers
❌
if (speed > 120)✅
#define MAX_SPEED 120U
if (speed > MAX_SPEED)Explicit Boolean Expressions
❌
if (x)✅
if (x != 0U)Reason:
-
Improves readability
-
Avoids implicit conversions
Use const Whenever Possible
const uint32_t MAX_SIZE = 64U;Reason:
-
Protect data from modification
-
Enables compiler optimizations
Proper Use of volatile
Used for:
-
Hardware registers
-
ISR-shared variables
volatile uint32_t * const GPIO_ODR = (uint32_t *)0x40020014;Pointer Arithmetic is Restricted
❌
ptr = ptr + 5;✅
ptr = &array[5];No Implicit Function Declarations
All functions must be declared before use.
Header Files Rules
-
Header files contain declarations only
-
No function definitions
-
Use include guards
#ifndef GPIO_H
#define GPIO_H
void GPIO_Init(void);
#endifAvoid Macro Side Effects
❌
#define SQR(x) x*x✅
#define SQR(x) ((x) * (x))Use Standard Library Carefully
Some standard library functions are restricted or forbidden:
-
printf (timing issues)
-
strcpy / strcat (buffer overflow)
Preferred safer alternatives or custom drivers.
No Mixing Signed and Unsigned Types
❌
uint32_t a;
int32_t b;
if (a > b)✅
if (a > (uint32_t)b)Limit Scope of Variables
void func(void)
{
uint32_t i = 0U; // local, limited scope
}Use Enumerations Carefully
-
Do not assume enum size
-
Do not mix enums with integers
Rule Classification
| Type | Meaning |
|---|---|
| Mandatory | Must never be violated |
| Required | Should not be violated |
| Advisory | Recommended |
Important Industry Note
✔ No real project follows MISRA 100% ✔ Violations must be documented using:
MISRA Deviation Report
This is completely normal in industry.
Final Summary
MISRA-C is not about making code longer.
It is about making code:
-
Predictable
-
Reviewable
-
Safe
-
Maintainable
If you master MISRA-C, you are ready for:
-
Professional Embedded projects
-
Automotive interviews
-
Safety-critical firmware design
Author target: Embedded Systems Engineers
Comments
Loading the discussion…