Avoid Violations of MISRA C Rules 8.x
Note
Starting in a future release, Code prover will not support checking MISRA C:2012 rules. Migrate to Bug Finder for checking these coding rules. See Avoid Violations of MISRA C Rules 8.x.
MISRA C™:2012 and 2023 rules 8.1-8.14 enforce good coding practices surrounding declarations and definitions. If you follow these practices, you are less likely to have conflicting declarations or to unintentionally modify variables.
If you do not follow these practices during coding, your code might require major changes later to be MISRA C-compliant. You might have too many MISRA C violations. Sometimes, in fixing a violation, you might violate another rule. Instead, keep these rules in mind when coding. Use the MISRA C:2012 or MISRA C:2023 checker to spot any issues that you might have missed.
Explicitly specify all data types in declarations.
Avoid implicit data types like this declaration of
k
:extern void foo (char c, const k);
Instead use:
extern void foo (char c, const int k);
That way, you do not violate
MISRA C:2012 Rule 8.1
orMISRA C:2023 Rule 8.1
.When declaring functions, provide names and data types for all parameters.
Avoid declarations without parameter names like these declarations:
extern int func(int); extern int func2();
Instead use:
extern int func(int arg); extern int func2(void);
That way, you do not violate
MISRA C:2012 Rule 8.2
orMISRA C:2023 Rule 8.2
.If you want to use an object or function in multiple files, declare the object or function once in only one header file.
To use an object in multiple source files, declare it as
extern
in a header file. Include the header file in all the source files where you need the object. In one of those source files, define the object. For instance:/* header.h */ extern int var;
/* file1.c */ #include "header.h" /* Some usage of var */
/* file2.c */ #include "header.h" int var=1;
To use a function in multiple source files, declare it in a header file. Include the header file in all the source files where you need the function. In one of those source files, define the function.
That way, you do not violate:
If you want to use an object or function in one file only, declare and define the object or function with the
static
specifier.Make sure that you use the
static
specifier in all declarations and the definition. For instance, this functionfunc
is meant to be used only in the current file:static int func(void); static int func(void){ }
That way, you do not violate:
If you want to use an object in one function only, declare the object in the function body.
Avoid declaring the object outside the function.
For instance, if you use
var
infunc
only, do declare it outside the body offunc
:int var; void func(void) { var=1; }
Instead use:
void func(void) { int var; var=1; }
That way, you do not violate:
If you want to inline a function, declare and define the function with the
static
specifier.Every time you add
inline
to a function definition, addstatic
too:static inline double func(int val); static inline double func(int val) { }
That way, you do not violate
MISRA C:2012 Rule 8.10
orMISRA C:2023 Rule 8.10
.When declaring arrays, explicitly specify their size.
Avoid implicit size specifications like this:
extern int32_t array[];
Instead use:
#define MAXSIZE 10 extern int32_t array[MAXSIZE];
That way, you do not violate
MISRA C:2012 Rule 8.11
orMISRA C:2023 Rule 8.11
.When declaring enumerations, try to avoid mixing implicit and explicit specifications.
Avoid mixing implicit and explicit specifications. You can specify the first enumeration constant explicitly, but after that, use either implicit or explicit specifications. For instance, avoid this type of mix:
enum color {red = 2, blue, green = 3, yellow};
Instead use:
enum color {red = 2, blue, green, yellow};
That way, you do not violate
MISRA C:2012 Rule 8.12
orMISRA C:2023 Rule 8.12
.When declaring pointers, point to a
const
-qualified type unless you want to use the pointer to modify an object.Point to a
const
-qualified type by default unless you intend to use the pointer for modifying the pointed object. For instance, in this example,ptr
is not used to modify the pointed object:char last_char(const char * const ptr){ }
That way, you do not violate
MISRA C:2012 Rule 8.13
orMISRA C:2023 Rule 8.13
.