== Why is this an issue?

The scope of objects shall be restricted to functions where possible. File scope shall only be used where objects need to have either internal or external linkage. Where objects are declared at file scope MISRA C 2004 Rule 8.10 applies. It is considered good practice to avoid making identifiers global except where necessary.


Whether objects are declared at the outermost or innermost block is largely a matter of style.


=== Noncompliant code example

[source,cpp]
----
int temp;
int function() {
  temp = someFunction1();
  temp += someFunction2();
  // ...
  return temp;
}
----


=== Compliant solution

[source,cpp]
----
int function() {
  int temp;
  temp = someFunction1();
  temp += someFunction2();
  // ...
  return temp;
}
----


== Resources

* MISRA C:2004, 8.7 - Objects shall be defined at block scope if they are only accessed from within a single function.
* MISRA C:2012, 8.9 - An object should be defined at block scope if its identifier only appears in a single function
* https://wiki.sei.cmu.edu/confluence/x/z9YxBQ[CERT, DCL19-C.] - Minimize the scope of variables and functions


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Move this declaration inside the body of function "xxx"


'''
== Comments And Links
(visible only on this page)

=== relates to: S806

=== on 17 Oct 2014, 13:35:01 Ann Campbell wrote:
\[~freddy.mallet] this rule overlaps the {cpp} version: RSPEC-806

=== on 17 Oct 2014, 13:35:48 Ann Campbell wrote:
Nicely done [~samuel.mercier]. Good SQALE choice too.

endif::env-github,rspecator-view[]
