Search code examples
cfunction-definition

Why conditionally compile an obsolete K&R definition versus standard C definition?


I came across this code in the wild and got very confused.

#ifdef __STDC__
    double cos(double x)
#else
    double cos(x)
    double x;
#endif
{
    // ...
}

I fail to see why the latter in the #else would be preferable in any circumstances. Are there any reasons to write function definitions as above as opposed to the very standard and widely accepted way as shown below?

double cos(double x) {
    // ... 
}

Solution

  • It may be necessary to use the latter form when compiling code with an ancient compiler or when compiling with a modern compiler but it is necessary to use an ancient version of the C language because some of the code being compiled uses old C features that are incompatible with modern C. Either of these is exceptionally rare these days.