C/C++:error: return-statement with a value, in function returning ‘void’ [-fpermissive]

created at 06-21-2022 views: 81

example code

#include <stdio.h>
void fun()
{
        printf("abc");
        return 1;
}
int main()
{
        fun();
}

Compiled by gcc, according to the C standard

Prompt a warning; not an error; a return value of type int was given when a void return type was required.

return.c: In function ‘fun’:
return.c:5:9: warning: 'return' with a value, in function returning void
return 1;
^
return.c:2:6: note: declared here
void fun()

g++, according to the C++ standard

An error is displayed; if you want to eliminate this error, you can use fpermissive to downgrade the error to a warning.

return.c: In function ‘void fun()’:
return.c:5:9: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
return 1;
^

-fpermissive

This explanation has several terms to understand; * downgrade: * examination: * conformant: consistency here refers to whether it conforms to the standard. Meets the definition of ABI. * nonconformant: may lead to unknown, undefined errors if inconsistent.

Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive allows some nonconforming code to compile.

So the final explanation is that adding this option allows the compilation to pass, but there is a chance of an unknown, undefined error.

created at:06-21-2022
edited at: 06-21-2022: