Following is possible with gcc and g++:
#include <math.h>
double sin(double)
{
return 1;
}
int main()
{
sin(1);
return 1;
}
Why I don't get any warnings like:
sin prevously defined in math.h ...
when I compile with -Wall -pedantic -ansi.
Because it isn't actually defined in <math.h>, only declared; the header
has[1] something like:
--------
double sin(double);
--------
This means "There's a function called sin, that takes a double and
returns a double, and it's defined somewhere else". When the compiler
sees your definition it assumes that this is the "somewhere else".
Why is it possible to overwrite the definition of sin,
is this part of the standard?
No, sort of.
By defining a function with the same name as a standard library function,
you're invoking undefined behavior, which means that the compiler is
no longer bound by the requirements the language definition places on
it (because your code doesn't conform to the requirements the language
definition places on _it_); it's allowed to do whatever the implementor
wants it to do in this case (usually either "whatever's convenient"
or "whatever this other standard that does cover this case defines",
but things like "make demons fly out of the programmers nose" aren't
forbidden, only extremely difficult to implement), and doesn't need to
warn you about it.
What I suspect GCC is actually doing is not linking with the math library
at all and not whining about not being able to find it because you gave it
a definition of the only function you use. What I suspect it would have
done if you told it to link the math library is let your sin() override
the one in the library and call the one you defined anyways. The details
of exactly what it's doing and why are beyond the scope of comp.lang.c,
but if you really want to know a newsgroup that discusses GCC, or possibly
one that discusses unix programming (I'm not sure whether this is a GCC
thing or a unix thing), would be able to answer questions about that.
Secondly the definition (not declaration) of double sin(double)
misses a variable!
Is this ok, when the variable is not referenced in the code?
In C++, yes. In C, no. I suspect you told GCC that it was C++ that it
was compiling and not C. (It usually makes this decision based on the
extension of the file, unless you use appropriate arguments to force it
to do otherwise - the name of the compiler executable you invoke doesn't
affect the language it compiles.)
dave
[1] Actually, to be pedantically correct, this should say "#including
the header is the equivalent of inserting something that has"; the
header need not be a real entity that actually contains anything -
it's perfectly valid for the compiler to interpret "#include <math.h>"
to mean "Copy the type declarations, function declarations, and macro
definitions from table 7-12 into the live-symbols table" and not have
the header exist as anything other than a pre-digested symbol table.
But if you catch us in between the pedantic nitpicking we like to do,
most of us will admit that almost all (all?) real implementations
actually have a file somewhere that they read instead of doing this.