65 Misra C Rules With Examples Pdf
Misra C Rules with Examples PDF
Introduction
The Misra C rules are a set of guidelines for writing code in the C programming language that aim to improve code quality, portability, and maintainability. These rules were developed by the Motor Industry Software Reliability Association (MISRA) to address common coding issues and potential pitfalls in C programming.
Why are Misra C Rules Important?
Misra C rules are important for several reasons:
- They help improve code quality by promoting best practices and reducing the likelihood of errors and bugs.
- They enhance code portability by ensuring that code written in accordance with the rules can be easily migrated to different platforms and compilers.
- They make code more maintainable by establishing a consistent coding style and structure, making it easier for developers to understand and modify the code.
- They contribute to software safety and reliability by reducing the potential for undefined behavior and vulnerabilities.
Examples of Misra C Rules
Let's take a look at some examples of Misra C rules:
Rule 1.1: A project shall not contain unreachable code
This rule states that all code within a project must be reachable and serve a purpose. Unreachable code can be a result of incomplete code removal or code that is never executed. Here's an example:
#include int main() { int x = 10; if (x > 5) { printf("x is greater than 5\n"); } return 0; printf("This line will never be executed\n"); }
In this example, the line printf("This line will never be executed\n");
is unreachable because it appears after the return
statement. To comply with Rule 1.1, the unreachable code should be removed or rewritten to be reachable.
Rule 2.2: Source code shall only use C-style comments
This rule states that only C-style comments (i.e., //
and /* */
) should be used in the source code, and other comment styles should be avoided. Here's an example:
#include int main() { int x = 10; // This is a C++-style comment (not allowed by Misra C) return 0; }
In this example, the line // This is a C++-style comment
violates Rule 2.2. To comply with the rule, the comment should be rewritten using C-style comments.
Rule 5.1: External identifiers shall be distinct
This rule states that all external identifiers (e.g., function names, global variables) within a project shall have distinct names. Here's an example:
#include void printHello() { printf("Hello, World!\n"); } int main() { printHello(); void printHello() { printf("Hello again!\n"); } printHello(); return 0; }
In this example, there are two external identifiers named printHello
, which violates Rule 5.1. To comply with the rule, the identifiers should have distinct names.
Rule 18.1: A pointer resulting from arithmetic shall have been obtained from a pointer that pointed to an element of the same array
This rule states that a pointer resulting from arithmetic operations (e.g., addition, subtraction) should have been obtained from a pointer that originally pointed to an element of the same array. Here's an example:
#include int main() { int numbers[] = {1, 2, 3, 4, 5}; int* ptr = numbers; int* invalidPtr = ptr + 2; printf("%d\n", *invalidPtr); return 0; }
In this example, the line int* invalidPtr = ptr + 2;
violates Rule 18.1 because ptr
does not point to an element of the same array as invalidPtr
. To comply with the rule, the code should be modified to ensure that the pointer resulting from arithmetic operations is derived from a valid pointer within the same array.
Conclusion
Adhering to the Misra C rules can greatly benefit C programmers by improving code quality, portability, and maintainability. These rules provide guidance on avoiding common pitfalls and promoting best practices. By following the rules and utilizing the provided examples, developers can write more reliable and robust C code.