MISRA Discussion Forums
Rule 17.4 and main - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.17 Functions (https://forum.misra.org.uk/forumdisplay.php?fid=172)
+---- Thread: Rule 17.4 and main (/showthread.php?tid=1582)



Rule 17.4 and main - hummelvario - 01-10-2021

Hello,
we are facing a conflict between armclang compiler warning and mandatory MISRA Rule.
The armclang compiler V6.16 seems to require "int main", but if we adhere to Rule 17.4 adding "return 0" at the end of main it is leading to a compiler warning.
(warning: 'return' will never be executed [-Wunreachable-code-return])

Who is having the higher priority here to comply with MISRA?
Is it required to deactivate / ignore the compiler warning at this point?

According to the C Standard, 5.1.2.2.3, paragraph 1 [ISO/IEC 9899:2011], "Reaching the } that terminates the main function returns a value of 0."
Is there another possibility to comply with the MISRA Rule 17.4?


RE: Rule 17.4 and main - misra-c - 27-11-2021

The MISRA C:2012 guidelines do not require an "int" return type from "main". The documentation for directive 1.1 states that main is commonly declared as "void main (void )" in freestanding implementations.

Therefore it is permissable to declare main to have a void return type. Your compiler may be configurable to permit a void return or the compiler warning noted, but ignored. For example, the clang compiler warning can be suppressed by using  -Wno-main-return-type

However, it is appreciated that not all compilers may have this option and the MISRA Working Group are likely to add an exception to rule 17.4 which will permit the use of  "main" with no return statement in the next Technical Corrigendum.


RE: Rule 17.4 and main - misra-c - 01-07-2024

MISRA C:2012 TC2 adds an exception to permit the absence of the final return from main()