[previous] MISRA-C:2004  Rule  8.11:  (Required) [next] The static storage class specifier shall be used in definitions and declarations of objects and functions that have internal linkage.
The static and extern storage class specifiers can be a source of confusion. It is good practice to apply the static keyword consistently to all declarations of objects and functions with internal linkage.

Example Code:


/*******************************************************************************
Note that the rules by which linkage are defined were modified in
Technical Corrigendum 1 of the ISO C standard,

1. If the declaration of a file scope identifier for an object contains the
   storage class specifier static, the identifier has internal linkage.

2. If the declaration of an identifier for an object or function contains
   the storage-class specifier extern, if a prior declaration of that
   identifier is visible and specifies internal or external linkage, the
   linkage of the identifier at the latter declaration becomes the same
   as the linkage specified at the prior declaration.

3. If the declaration of an identifier for an object or function contains
   the storage-class specifier extern and there is no visible prior declaration
   of the identifier, the identifier has external linkage.

4  If the declaration of an identifier for a function has no storage class
   specifier, its linkage is determined exactly as if it were declared with
   the storage class specifier extern.

5. If the declaration of an identifier for an object has file scope and no
   storage-class specifier, its linkage is external.

********************************************************************************/

/* PRQA S 2021,3205,3207,3219,3222,3447,3408 ++ */

#include "misra.h"
#include "m2cmex.h"

static S16 obj_0811a;                  /* Internal linkage (1) */
extern S16 obj_0811a;                  /* Internal linkage (2) - MISRA Violation */

extern S16 obj_0811b;                  /* External linkage (3) */
       S16 obj_0811b;                  /* External linkage (5) */

       S16 obj_0811c;                  /* External linkage (5) */
extern S16 obj_0811c;                  /* External linkage (2) */

static S16 obj_0811d;                  /* Internal linkage (1) */
       S16 obj_0811d;                  /* External linkage (5) - MISRA Violation - Rule 1.2 */

       S16 obj_0811e;                  /* External linkage (5) */
static S16 obj_0811e;                  /* Internal linkage (1) - MISRA Violation - Rule 1.2 */

extern S16 obj_0811f;                  /* External linkage (3) */
static S16 obj_0811f;                  /* Internal linkage (1) - MISRA Violation - Rule 1.2 */



extern S16 test_0811( void );          /* External linkage (3) */

static S16 test_0811( void )           /* Internal linkage (1) * MISRA Violation - Rule 1.2 */
{
    extern S16 a0811;                  /* External linkage (3) */
    static S16 a0811 = 0;              /* Internal linkage (1) * MISRA Violation - Rule 1.1 */

    extern S16 obj_0811a;              /* Internal linkage (2) */

    extern S16 obj_0811b;              /* External linkage (2) */

    return 0;
}


QAC messages that encompass this guideline:

3224 This identifier has previously been declared with internal linkage but is not declared here with the static storage class specifier.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents