Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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) {
    // ... 
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading