Main Content

CERT C: Rec. PRE04-C

Do not reuse a standard header file name

Since R2025a

Description

Rule Definition

Do not reuse a standard header file name.1

Polyspace Implementation

This checker checks for Standard header file name reused.

Examples

expand all

Issue

The issue occurs when you include a user-defined header file that has the same name as one of the standard library header files listed in this table.

<assert.h><inttypes.h><signal.h> <stdint.h> <threads.h>
<complex.h><iso646.h> <stdalign.h><stdio.h> <time.h>
<ctype.h><limits.h><stdarg.h><stdlib.h><uchar.h>
<errno.h><locale.h><stdatomic.h><stdnoreturn.h><wchar.h>
<fenv.h><math.h><stdbool.h><string.h><wctype.h>
<float.h><setjmp.h><stddef.h><tgmath.h> 

Polyspace reports a violation for header files that are enclosed in quotes ("") when those header files reuse the name of one of the files in the preceding table.

Risk

If the search path for included source files has a file with the same name as one of the standard library header files, the behavior is undefined.

Fix

Do not reuse the file names of the standard library headers when including a user-defined header.

Example — Reuse of a Standard Header File Name
#include <stdio.h>
#include "math.h" // Noncompliant

double area(double radius)
{
  double pi = MY_PI; // custom MY_PI macro defined in math.h

  return pi * radius * radius;
}

In this example, Polyspace flags the inclusion of "math.h" because it reuses the name of a standard library header.

Correction — Use Unique Name for User-Defined Header
#include <stdio.h>
#include "custom_math.h" // Compliant

double area(double radius)
{
  double pi = MY_PI; // custom MY_PI macro defined in custom_math.h

  return pi * radius * radius;
}

One possible correction is to use a distinctive name for user-defined header files, such as custom_math.h.

Check Information

Group: Rec. 01. Preprocessor (PRE)

Version History

Introduced in R2025a


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.