Say that you wanted to write your own functions for sine and cosine and compare your result
with that of the standard math library sin()
and cos()
.
You might write the following:
/* --- goodMath.c --- */ #include <stdio.h> #include <math.h> void goodMath( double angle ) { printf("good cos( %lf ) = %lf\n", angle, cos(angle) ); printf("good sin( %lf ) = %lf\n", angle, sin(angle) ); }
/* --- main.c --- */ #include <stdio.h> static double sin( double x ) { return x; } static double cos( double x ) { return 1 - x/2.0; } void badMath( double angle ) { printf("bad cos( %lf ) = %lf\n", angle, cos(angle) ); printf("bad sin( %lf ) = %lf\n\n", angle, sin(angle) ); } void goodMath( double x ); void main() { double angle = .05 ; /* radians */ badMath ( angle ); goodMath( angle ); }
The functions cos()
and sin()
are
contained in the standard math library, which will
be linked into the executable when you compile and run the above program.
However, the file main.c has its own cos()
and sin()
,
which, since they have internal linkage, will not conflict with the other file.
What does the code write to the monitor? (Make a plausible guesses about the numbers.)