Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to check if a function is defined in C?

void f();
int main(int argc, char** argv)
{
    if (f)
    {
        // other code
    }
}

With VS2017, the linker complaint about unsolved external symbol, while it works with GCC. According to C99 spec, is it valid? Or it's implementation detail?

like image 379
user20291889 Avatar asked Oct 15 '25 21:10

user20291889


1 Answers

C standard requires that every symbol should be defined exactly once in a correct program, but does not require any diagnostic if the rule is not observed. So if you declare a function that is never defined in any compilation unit any use of that function is beyond C specification.

The gcc compiler is known to have plenty of extensions, some of which are also accepted by clang. If you know that you will only use gcc, you can use them, if you want to write portable programs you should not.

like image 69
Serge Ballesta Avatar answered Oct 17 '25 14:10

Serge Ballesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!