kids encyclopedia robot

MISRA C facts for kids

Kids Encyclopedia Facts

MISRA C is a special set of rules for writing computer programs using the C programming language. These rules were created by a group called The MISRA Consortium. Their main goal is to help programmers write code that is super safe, secure, easy to move to different computers (called portability), and very reliable. This is especially important for embedded systems, which are computers built into other devices, like cars or medical equipment.

There are also rules for another programming language called C++, but this article focuses on C.

History of MISRA C Rules

The idea for MISRA C started in 1997. The first official set of rules came out in 1998. Since then, they've been updated a few times to make them even better:

  • 1998: The very first rules were published.
  • 2004: A second, updated version was released.
  • 2012: The third version came out. This one introduced new types of rules called "directives."
  • 2016 & 2020: More guides were added to help people understand how to follow the rules.

In the first two versions (1998 and 2004), all the suggestions were just called "Rules." But with MISRA C:2012, they added "Directives." Directives are like guidelines that are a bit more flexible or relate to how you manage your coding process.

Who Uses MISRA C?

Even though MISRA C first started for the car industry, it's now used by many different groups. It's seen as a great way to write high-quality, safe software. For example, it's used in:

  • Cars: To make sure the software controlling your car is safe and reliable.
  • Airplanes: For projects like the Joint Strike Fighter program, their coding rules are based on MISRA C.
  • Space: NASA's Jet Propulsion Laboratory uses MISRA C rules for their space missions.
  • Medical Devices: To ensure devices like pacemakers or hospital equipment work perfectly.
  • Trains and Telecom: Many other important industries also use these rules.

Organizations like ISO 26262 (which deals with safety in road vehicles) and AUTOSAR (a partnership for car software) also recommend using MISRA C.

Understanding the Rules

When a new software project starts, it's best to use the newest MISRA standard. Older versions are still available for older projects that need them.

Types of Guidelines

Each MISRA guideline is sorted into one of three types: Mandatory, Required, or Advisory.

  • Mandatory: You must always follow these rules. No exceptions!
  • Required: You should follow these rules. If you absolutely can't, you need to explain why in a special document called a "Deviation."
  • Advisory: These are good ideas and best practices. It's smart to follow them, but it's less strict than Mandatory or Required rules.

How Rules are Grouped

The rules are also grouped into different categories to make sense:

  • Avoiding Differences: Some rules help make sure your code works the same way on different computers or with different compilers (tools that turn your code into a program). For example, they might suggest using specific number types that are always the same size.
  • Avoiding Problems: Some rules tell you to avoid parts of the C language that are known to cause issues or fail often.
  • Easy to Understand: Other rules help make your code clear, easy to read, and simple to fix if something goes wrong. This includes rules about how you name things or add comments to your code.
  • Best Practices: These are general tips for writing good, clean code.
  • Complexity Limits: Some rules help keep your code from becoming too complicated, which can make it harder to understand and test.

Following the Rules (Compliance)

To say that a piece of software "complies" with MISRA C, it means it follows all the rules.

What "Compliance" Means

  • All Mandatory rules must be followed.
  • All Required rules must either be followed, or you must have a formal "Deviation" explaining why you didn't follow them.
  • Advisory rules are good to follow, but you don't need a formal "Deviation" if you don't. Still, it's a good idea to write down why you chose not to follow them.

Deviations: When You Don't Follow a Rule

Sometimes, a software engineer might need to bend a "Required" MISRA C rule for a very good reason. This is called a "deviation." When this happens, they must:

  • Write down which rule they are not following.
  • Explain why they are not following it.
  • Show proof that not following the rule won't make the system unsafe.

This documentation helps everyone understand why a rule was skipped and ensures safety is still the top priority.

Different Versions of MISRA C

MISRA C:1998

This was the very first version, published in 1998. It had 127 rules.

MISRA C:2004

Published in 2004, this version had many big changes. It had 142 rules, divided into 21 topics, from how the program works with its environment to dealing with errors.

MISRA C:2012

The newest main version came out in 2013. It added support for newer versions of the C language (like C99) and made it easier to follow the rules.

  • It has 143 rules and 16 "directives" (those more flexible guidelines).
  • Each rule is still classified as Mandatory, Required, or Advisory.

Updates to MISRA C:2012

  • Amendment 1 (2016): Added 14 new rules specifically about computer security to help make code even safer from attacks.
  • Amendment 2 (2020): Updated the rules to work better with the very latest versions of the C language (C11/C18).

Tools to Help with MISRA C

Many software tools can help programmers check if their code follows MISRA C rules. These tools are super helpful because they can automatically scan millions of lines of code.

Some tools check code by looking at it without running it (called static code analysis). Others check it while it's running (called dynamic code analysis).

Here are some examples of tools that help check for MISRA compliance:

  • Astrée
  • CodeSonar
  • Cppcheck (an open-source tool)
  • Helix QAC
  • Klocwork
  • Parasoft C/C++test
  • Polyspace
  • PVS-Studio
  • SonarQube
  • Understand

Some C/C++ compilers (the programs that turn your code into a working application) also have built-in features to help you follow MISRA rules, like those from Green Hills Software and IAR Systems.

Is MISRA C Perfect?

Some studies have looked into how effective MISRA C rules are. For example, some researchers have suggested that while MISRA C is helpful, it might sometimes flag too many "false positives" – meaning it points out things as problems that aren't actually problems. This can make it harder for programmers to find the real issues.

However, the goal of MISRA C is always to improve and help create the safest and most reliable software possible, especially for critical systems where mistakes can be very dangerous.

See also

  • Programming style
kids search engine
MISRA C Facts for Kids. Kiddle Encyclopedia.