Friday, October 28, 2022

[SOLVED] Why does clang c89 have powf, but gcc c89 doesn't?

Issue

Isn't the c89 standard supposed to be consistent ?

I'm compiling with gcc -W -Wall -std=c89 -pedantic -O3

On macOS, gcc is an alias of clang it seems : gcc --version returns Apple clang version 14.0.0 (clang-1400.0.29.201). It gives no warnings about powf, and the program behaves as expected.

Whereas on linux it's the real deal. It gives all the errors / warnings expected and doesn't compile.


Solution

I'm compiling with -c89, and on Linux I have to define powf myself but on macOS it's already defined. Why is there a difference ?

The specifications for the standard library of C89 include a pow() function with arguments of type double that returns a double, but none for a powf() function. C99 added powf() as an analog that accepts arguments of type float and returns a float.

How different compilers for different machines handle that when asked to compile in a mode that requests conformance with a specific version of the language specification is specific to those compilers. That's all that really can be said about why you observe a difference.

But do note that the difference you observe might not be the difference you think you observe. If compiling in C89 mode means that no declaration of powf() is provided by math.h then that does not imply that C89 programs that call that function will not compile. On the contrary, such programs should still compile, but their calls to powf() will produce undefined behavior, probably incorrect behavior, as a result of the powf() function that is in fact present in the system libraries not having the signature that a C89 processor should infer from a call to that function when no prototype is in scope.

Isn't the c89 standard supposed to be consistent ?

The C89 standard is a singular document. It cannot fail to be consistent. But how different compilers implement c89 conformance modes is not consistent.



Answered By - John Bollinger
Answer Checked By - Terry (WPSolving Volunteer)