Main Content

MISRA C++:2023 Rule 6.0.4

The identifier main shall not be used for a function other than the global function main

Since R2024b

Description

Rule Definition

The identifier main shall not be used for a function other than the global function main. 1

Rationale

Typically, the main function lives in the global namespace and acts as the entry point to a program. The use of main in other contexts is confusing and unexpected.

Polyspace Implementation

The rule checker reports a violation if you use the identifier main in a namespace other than the global namespace.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstdint>

class ExampleA {
public:
    void main(void) {}      //Noncompliant
};

int32_t main() {            //Compliant
    //...
    return 0;
}

namespace Backups {
    int32_t main() {        //Noncompliant
        //...
        return 0;
    }
}

The use of main in a namespace other than the global namespace violates the rule.

Check Information

Group: Basic Concepts
Category: Required

Version History

Introduced in R2024b


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.