Search code examples
cundefined-behaviorlinkagefunction-declaration

Linking functions with different return types


0.c

int test();
int main(){
 return test();
}

1.c

void test(){
 //
}

Above compiles fine with gcc 0.c 1.c and main returns 0. Is this undefined behaviour? as test technically doesn't return anything.


Solution

  • Opposite to C++ C does not stores in external names of functions their return types and argument types. It only specifies that (relative to the provided code) there is an external name of the function test used for linkage.

    As for C++ then for example in the documentation for MS VC++ there is written about decorated names

    If you change the function name, class, calling convention, return type, or any parameter, the decorated name also changes. In this case, you must get the new decorated name and use it everywhere the decorated name is specified.

    Decorated names are implementation-defined in C++.

    As a result the provided code in the question has undefined behavior.