Main Content

CERT C: Rec. DCL20-C

Explicitly specify void when a function accepts no arguments

Since R2026a

Description

Explicitly specify void when a function accepts no arguments1

Polyspace Implementation

The rule checker checks for Use of function declarators with empty parentheses.

Examples

expand all

Issue

The rule checker reports a violation if a function declaration uses empty parenthesis () instead of (void) to indicate no parameters.

Risk

If a function is declared with an empty parameter list, the compiler does not check if the function is called with a parameter at all. At compile time, the compiler can accept a call to the function with arbitrary parameters. Such a function declaration is ambiguous and can result in issues such as undefined behavior and buffer overflow.

Fix

If a function accepts no arguments, then explicitly declare a void parameter in the function parameter list. Use the void parameter for both declaration and definition of a function.

Example

In this header file, the function myFunction() is declared using empty parentheses to indicate that the function accepts no argument. Polyspace® reports a violation.

// my_function.h
#ifndef MY_FUNCTION_H
#define MY_FUNCTION_H

void myFunction(); //Noncompliant

#endif // MY_FUNCTION_H

Correction

To fix this violation, use void in the argument list to indicate that the function myFunction accepts no arguments.

// my_function.h
#ifndef MY_FUNCTION_H
#define MY_FUNCTION_H

void myFunction(void); //Compliant

#endif // MY_FUNCTION_H

Check Information

Group: Rec. 02. Declarations and Initialization (DCL)
PQL Name: std.cert.DCL20_C

Version History

Introduced in R2026a


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.