Main Content

CERT C: Rule DCL31-C

Declare identifiers before using them

Description

Rule Definition

Declare identifiers before using them.1

Polyspace Implementation

The rule checker checks for these issues:

  • Types not explicitly specified.

  • Implicit function declaration.

This violation is reported on each use of a noncompliant identifier.

Examples

expand all

Issue

The rule checker flags situations where a function parameter or return type is not explicitly specified. To enable checking of this rule, use the value c90 for the option C standard version (-c-version).

Risk

In some circumstances, you can omit types from the C90 standard. In those cases, the int type is implicitly specified. However, the omission of an explicit type can lead to confusion. For example, in the declaration extern void foo (char c, const k);, the type of k is const int, but you might expect const char.

You might be using an implicit type in:

  • Object declarations

  • Parameter declarations

  • Member declarations

  • typedef declarations

  • Function return types

Example - Implicit Types
static foo(int a);  /* Non compliant */
static void bar(void);      /* Compliant */

In this example, the rule is violated because the return type of foo is implicit.

Issue

The issue occurs when you call a function before you declare or define it.

Risk

An implicit declaration occurs when you call a function before declaring or defining it. When you declare a function explicitly before calling it, the compiler can match the argument and return types with the parameter types in the declaration. If an implicit declaration occurs, the compiler makes assumptions about the argument and return types. For instance, it assumes a return type of int. The assumptions might not agree with what you expect and cause undesired type conversions.

Example - Function Not Declared Before Call
#include <math.h>

extern double power3 (double val, int exponent);
int getChoice(void);

double func() {
    double res;
    int ch = getChoice();
    if(ch == 0) {
        res = power(2.0, 10);    /* Non-compliant */
    }
    else if( ch==1) {
        res = power2(2.0, 10);   /* Non-compliant */
    }
    else {
        res = power3(2.0, 10);   /* Compliant */
        return res;
    }
}

double power2 (double val, int exponent) {
    return (pow(val, exponent));
}

In this example, the rule is violated when a function that is not declared is called in the code. Even if a function definition exists later in the code, the rule violation occurs.

The rule is not violated when the function is declared before it is called in the code. If the function definition exists in another file and is available only during the link phase, you can declare the function in one of the following ways:

  • Declare the function with the extern keyword in the current file.

  • Declare the function in a header file and include the header file in the current file.

Check Information

Group: Rule 02. Declarations and Initialization (DCL)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.