MISRA Discussion Forums

Full Version: Rule 8.4 - main function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Should this rule have an exception for main? I can't see how main is automatically excluded from this rule. Is the expectation that a prototype is provided for main?

Thanks.
I would think so, because the signature is mandatory and there is already an exception for main in Dir 4.6.
Rule 8.4 is required, not mandatory. Directive 4.6 isn't relevant here, as it isn't concerned with prior declaration.

Although main has external linkage, I think that the MISRA WG would deem it to have 'no linkage'. The rule and its rationale don't really apply to main, as it isn't called by another translation unit.

Going by the exact wording of the rule, however, I'm not sure whether the following is necessary to avoid a formal deviation:

Code:
int main(void);

int main(void)
{
    ...
}

I think that this would be confusing to someone maintaining the code.

Also, when I wrote 'prototype' in the original post, I really meant declaration in prototype form.
dg1980, I realise now that by signature, you meant prototype, not category, as I originally interpreted it.
This statement is incorrect:
"I think that the MISRA WG would deem it to have 'no linkage'".
Thank you for drawing our attention to this question. It was not the intention of the MISRA-C working group that rule 8.4 should apply to "main". We will be issuing a clarification.

In summary, a "main" function must contain a prototyped definition, but need not have a separate declaration.