How to force gcc compilator throw error when int main() have no return statement. This code compiles without any errors
#include<stdio.h>
int main(){
printf("Hi");
}
I am using
gcc -Wall -Wextra -std=c99 -Wreturn-type -Werror -pedantic-errors a.c
command for compilation
You can avoid the special treatment that main
receives by renaming it. Compiling with -Dmain=AlternateName -Werror=return-type
yields “error: control reaches end of non-void function”.
Naturally, you would do this as a special compilation to test for the issue and not use the object module resulting from this compilation. A second normal compilation without -Dmain=AlternateName
would be used to generate the object module.
This can't be disabled, as such behavior would be in violation of the C99 standard.
Section 5.1.2.2.3 of C99 states the following regarding the main
function:
If the return type of the
main
function is a type compatible withint
, a return from the initial call to the main function is equivalent to calling theexit
function with the value returned by themain
function as its argument; reaching the}
that terminates themain
function returns a value of 0. If the return type is not compatible withint
, the termination status returned to the host environment is unspecified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With